diff --git a/CHANGELOG.md b/CHANGELOG.md index a9013c5c64ac..ab0ecaecb438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -128,6 +128,10 @@ Every Module contains its own CHANGELOG.md. Please refer to the module you are i * (types) [#18695](https://github.com/cosmos/cosmos-sdk/pull/18695) Removed global configuration for txEncoder. * (server) [#18909](https://github.com/cosmos/cosmos-sdk/pull/18909) Remove configuration endpoint on grpc reflection endpoint in favour of auth module bech32prefix endpoint already exposed. +### Client Breaking Changes + +* (runtime) [#19040](https://github.com/cosmos/cosmos-sdk/pull/19040) Simplify app config implementation and deprecate `/cosmos/app/v1alpha1/config` query. + ### CLI Breaking Changes * (server) [#18303](https://github.com/cosmos/cosmos-sdk/pull/18303) `appd export` has moved with other genesis commands, use `appd genesis export` instead. diff --git a/UPGRADING.md b/UPGRADING.md index 8713095acb3c..6207265bc3e0 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -82,7 +82,7 @@ for more details. ### Params -* Params Migrations were removed. It is required to migrate to 0.50 prior to upgrading to .51. +* Params migrations were removed. It is required to migrate to 0.50 prior to upgrading to v0.51. ### SimApp @@ -112,13 +112,18 @@ clientCtx = clientCtx. Refer to SimApp `root_v2.go` and `root.go` for an example with an app v2 and a legacy app. +#### Dependency Injection + + + ### Modules #### `**all**` ##### Dependency Injection -Previously `cosmossdk.io/core` held functions `Invoke`, `Provide` and `Register` were moved to `cosmossdk.io/depinject/appmodule`. All modules using dependency injection must update their imports. +Previously `cosmossdk.io/core` held functions `Invoke`, `Provide` and `Register` were moved to `cosmossdk.io/depinject/appconfig`. +All modules using dependency injection must update their imports. ##### Genesis Interface diff --git a/api/cosmos/app/v1alpha1/query.pulsar.go b/api/cosmos/app/v1alpha1/query.pulsar.go index 7de26bd84a94..978824645c58 100644 --- a/api/cosmos/app/v1alpha1/query.pulsar.go +++ b/api/cosmos/app/v1alpha1/query.pulsar.go @@ -894,27 +894,27 @@ var file_cosmos_app_v1alpha1_query_proto_rawDesc = []byte{ 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x33, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x2e, 0x43, 0x6f, 0x6e, 0x66, - 0x69, 0x67, 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x32, 0x66, 0x0a, 0x05, 0x51, 0x75, - 0x65, 0x72, 0x79, 0x12, 0x5d, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x27, 0x2e, + 0x69, 0x67, 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x32, 0x69, 0x0a, 0x05, 0x51, 0x75, + 0x65, 0x72, 0x79, 0x12, 0x60, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x27, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x28, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x22, 0x00, 0x42, 0xc5, 0x01, 0x0a, 0x17, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, - 0x73, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x42, 0x0a, - 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x30, 0x63, 0x6f, - 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, - 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, - 0x61, 0x31, 0x3b, 0x61, 0x70, 0x70, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0xa2, 0x02, - 0x03, 0x43, 0x41, 0x58, 0xaa, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x41, 0x70, - 0x70, 0x2e, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0xca, 0x02, 0x13, 0x43, 0x6f, 0x73, - 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x70, 0x70, 0x5c, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, - 0xe2, 0x02, 0x1f, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x70, 0x70, 0x5c, 0x56, 0x31, - 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, - 0x74, 0x61, 0xea, 0x02, 0x15, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, 0x41, 0x70, 0x70, - 0x3a, 0x3a, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x22, 0x03, 0x88, 0x02, 0x01, 0x42, 0xc5, 0x01, 0x0a, 0x17, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, + 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, + 0x31, 0x42, 0x0a, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, + 0x30, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, + 0x69, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x76, 0x31, 0x61, + 0x6c, 0x70, 0x68, 0x61, 0x31, 0x3b, 0x61, 0x70, 0x70, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, + 0x31, 0xa2, 0x02, 0x03, 0x43, 0x41, 0x58, 0xaa, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0xca, 0x02, 0x13, + 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x70, 0x70, 0x5c, 0x56, 0x31, 0x61, 0x6c, 0x70, + 0x68, 0x61, 0x31, 0xe2, 0x02, 0x1f, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x70, 0x70, + 0x5c, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, + 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x15, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, + 0x41, 0x70, 0x70, 0x3a, 0x3a, 0x56, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/api/cosmos/app/v1alpha1/query_grpc.pb.go b/api/cosmos/app/v1alpha1/query_grpc.pb.go index c8d56e6cbe6e..b2151b37a09b 100644 --- a/api/cosmos/app/v1alpha1/query_grpc.pb.go +++ b/api/cosmos/app/v1alpha1/query_grpc.pb.go @@ -26,6 +26,7 @@ const ( // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type QueryClient interface { + // Deprecated: Do not use. // Config returns the current app config. Config(ctx context.Context, in *QueryConfigRequest, opts ...grpc.CallOption) (*QueryConfigResponse, error) } @@ -38,6 +39,7 @@ func NewQueryClient(cc grpc.ClientConnInterface) QueryClient { return &queryClient{cc} } +// Deprecated: Do not use. func (c *queryClient) Config(ctx context.Context, in *QueryConfigRequest, opts ...grpc.CallOption) (*QueryConfigResponse, error) { out := new(QueryConfigResponse) err := c.cc.Invoke(ctx, Query_Config_FullMethodName, in, out, opts...) @@ -51,6 +53,7 @@ func (c *queryClient) Config(ctx context.Context, in *QueryConfigRequest, opts . // All implementations must embed UnimplementedQueryServer // for forward compatibility type QueryServer interface { + // Deprecated: Do not use. // Config returns the current app config. Config(context.Context, *QueryConfigRequest) (*QueryConfigResponse, error) mustEmbedUnimplementedQueryServer() diff --git a/core/appmodule/README.md b/core/appmodule/README.md index 5d02cc25bdb3..f41dd41a9d64 100644 --- a/core/appmodule/README.md +++ b/core/appmodule/README.md @@ -4,5 +4,4 @@ This package defines what is needed for an module to be used in the Cosmos SDK. - -If you are looking at integrating Dependency injection into your module please see [depinject appmodule documentation](../../depinject/appmodule/README.md) +If you are looking at integrating Dependency injection into your module please see [depinject appconfig documentation](../../depinject/appconfig/README.md) \ No newline at end of file diff --git a/depinject/README.md b/depinject/README.md index e9ba563e6def..de58b49e28e0 100644 --- a/depinject/README.md +++ b/depinject/README.md @@ -12,15 +12,15 @@ sidebar_position: 1 `depinject` is particularly useful for developing blockchain applications: -* With multiple interdependent components, modules, or services. Helping manage their dependencies effectively. -* That require decoupling of these components, making it easier to test, modify, or replace individual parts without affecting the entire system. -* That are wanting to simplify the setup and initialisation of modules and their dependencies by reducing boilerplate code and automating dependency management. +* With multiple interdependent components, modules, or services. Helping manage their dependencies effectively. +* That require decoupling of these components, making it easier to test, modify, or replace individual parts without affecting the entire system. +* That are wanting to simplify the setup and initialisation of modules and their dependencies by reducing boilerplate code and automating dependency management. By using `depinject`, developers can achieve: -* Cleaner and more organised code. -* Improved modularity and maintainability. -* A more maintainable and modular structure for their blockchain applications, ultimately enhancing development velocity and code quality. +* Cleaner and more organised code. +* Improved modularity and maintainability. +* A more maintainable and modular structure for their blockchain applications, ultimately enhancing development velocity and code quality. * [Go Doc](https://pkg.go.dev/cosmossdk.io/depinject) @@ -38,9 +38,9 @@ Example: package main import ( - "fmt" + "fmt" - "cosmossdk.io/depinject" + "cosmossdk.io/depinject" ) type AnotherInt int @@ -49,21 +49,21 @@ func GetInt() int { return 1 } func GetAnotherInt() AnotherInt { return 2 } func main() { - var ( - x int - y AnotherInt - ) - - fmt.Printf("Before (%v, %v)\n", x, y) - depinject.Inject( - depinject.Provide( - GetInt, - GetAnotherInt, - ), - &x, - &y, - ) - fmt.Printf("After (%v, %v)\n", x, y) + var ( + x int + y AnotherInt + ) + + fmt.Printf("Before (%v, %v)\n", x, y) + depinject.Inject( + depinject.Provide( + GetInt, + GetAnotherInt, + ), + &x, + &y, + ) + fmt.Printf("After (%v, %v)\n", x, y) } ``` @@ -83,11 +83,11 @@ Consider the following example: package duck type Duck interface { - quack() + quack() } type AlsoDuck interface { - quack() + quack() } type Mallard struct{} @@ -97,7 +97,7 @@ func (duck Mallard) quack() {} func (duck Canvasback) quack() {} type Pond struct { - Duck AlsoDuck + Duck AlsoDuck } ``` @@ -105,15 +105,15 @@ And the following provider functions: ```go func GetMallard() duck.Mallard { - return Mallard{} + return Mallard{} } func GetPond(duck Duck) Pond { - return Pond{Duck: duck} + return Pond{Duck: duck} } func GetCanvasback() Canvasback { - return Canvasback{} + return Canvasback{} } ``` @@ -124,9 +124,9 @@ var pond Pond depinject.Inject( depinject.Provide( - GetMallard, - GetPond, - ), + GetMallard, + GetPond, + ), &pond) ``` @@ -138,12 +138,12 @@ However, if there are multiple implementations of the `Duck` interface, as in th var pond Pond depinject.Inject( - depinject.Provide( - GetMallard, - GetCanvasback, - GetPond, - ), - &pond) + depinject.Provide( + GetMallard, + GetCanvasback, + GetPond, + ), + &pond) ``` A specific binding preference for `Duck` is required. @@ -154,21 +154,21 @@ In the above situation registering a binding for a given interface binding may l ```go depinject.Inject( - depinject.Configs( - depinject.BindInterface( - "duck/duck.Duck", - "duck/duck.Mallard", - ), - depinject.Provide( - GetMallard, - GetCanvasback, - GetPond, - ), - ), - &pond) + depinject.Configs( + depinject.BindInterface( + "duck/duck.Duck", + "duck/duck.Mallard", + ), + depinject.Provide( + GetMallard, + GetCanvasback, + GetPond, + ), + ), + &pond) ``` -Now `depinject` has enough information to provide `Mallard` as an input to `APond`. +Now `depinject` has enough information to provide `Mallard` as an input to `APond`. ### Full example in real app diff --git a/depinject/appmodule/README.md b/depinject/appconfig/README.md similarity index 94% rename from depinject/appmodule/README.md rename to depinject/appconfig/README.md index ebcdeaf74c3e..b95f3e8ebc0a 100644 --- a/depinject/appmodule/README.md +++ b/depinject/appconfig/README.md @@ -37,11 +37,11 @@ message Module { Once we have a module config object, we need to register depinject providers and invokers for the module using the `cosmossdk.io/core/appmodule` package. -At the most basic level, we must define an `init` function in the package listed as the `go_import` in the module descriptor. This `init` function must call `appmodule.Register` with an empty instance of the config object and some options for initializing the module, ex: +At the most basic level, we must define an `init` function in the package listed as the `go_import` in the module descriptor. This `init` function must call `appconfig.RegisterModule` with an empty instance of the config object and some options for initializing the module, ex: ```go func init() { - appmodule.Register(&modulev1.Module{}, + appconfig.RegisterModule(&modulev1.Module{}, // options ) } @@ -53,14 +53,14 @@ A `depinject` "provider" is a function which takes dependencies from other modul other modules to use as dependencies. A `depinject` "invoker" is function which takes optional dependencies as inputs, returns no outputs, and is run at the end of initializing the dependency graph. Providers are much more common than invokers and should be the preferred method of wiring up modules when possible. Providers and invokers can be registered -for modules by using `appmodule.Provide` and `appmodule.Invoke` to create options which get passed -to `appmodule.Register` in the module `init` function, ex: +for modules by using `appconfig.Provide` and `appconfig.Invoke` to create options which get passed +to `appconfig.RegisterModule` in the module `init` function, ex: ```go func init() { - appmodule.Register(&modulev1.Module{}, - appmodule.Provide(provideSomething, provideSomethingElse), - appmodule.Invoke(invokeSomething), + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(provideSomething, provideSomethingElse), + appconfig.Invoke(invokeSomething), ) } ``` @@ -79,7 +79,7 @@ func init() { #### Regular Golang Types Regular golang types (besides the special cases described above) can be provided as both input and output parameters -to providers and invokers. For `depinject` to match an output parameter of one provider to an input parameter of +to providers and invokers. For `depinject` to match an output parameter of one provider to an input parameter of another, there must be an exact match for the type unless the input parameter is an input type. For instance, if a provider defines a dependency on `Foo` and some module provides `*Foo`, these two types will not match and there will be an error. @@ -133,7 +133,7 @@ bet. If `depinject.ModuleKey` is used as input parameter for a provider, the provider function will be treated as a "module-scoped provider" which means that the provider function will be called exactly once every time one of its outputs is needed by a module so that the provider can provide a unique instance of the dependency to -each module. +each module. Module-scoped dependencies should be used to provide dependencies which are private and unique to each module. Examples of these are store keys and param subspaces. @@ -177,7 +177,7 @@ when ordering *really* doesn't matter (which is rare). ### Resolving Circular Dependencies -Circular dependencies are inevitable to crop up and there are ways to avoid them. While `depinject` cannot handle +Circular dependencies are inevitable to crop up and there are ways to avoid them. While `depinject` cannot handle circular dependency graphs of providers, many of the above tools are designed to enable satisfying circular dependencies between modules. @@ -193,7 +193,7 @@ the staking module can define an invoker which depends on `map[string]StakingHoo satisfy this dependency graph which allows staking and slashing to depend on each other in this order: * provide staking keeper -> slashing keeper -* provide slashing keeper wrapped as `StakingHooksWrapper` +* provide slashing keeper wrapped as `StakingHooksWrapper` * get `map[string]StakingHooksWrapper` and the staking keeper and wire them together ## 3. Testing and Debugging The Module @@ -212,8 +212,8 @@ var appConfig []byte var AppConfig = appconfig.LoadYAML(appConfig) func TestModule(t *testing.T) { - var keeper Keeper - assert.NilError(t, depinject.Inject(AppConfig, &keeper)) + var keeper Keeper + assert.NilError(t, depinject.Inject(AppConfig, &keeper)) } ``` diff --git a/depinject/appconfig/config_test.go b/depinject/appconfig/config_test.go index 8595f96b5722..1bb799749d70 100644 --- a/depinject/appconfig/config_test.go +++ b/depinject/appconfig/config_test.go @@ -12,7 +12,6 @@ import ( "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" - "cosmossdk.io/depinject/appmodule" internal "cosmossdk.io/depinject/internal/appconfig" "cosmossdk.io/depinject/internal/appconfig/testpb" ) @@ -102,7 +101,7 @@ modules: assert.NilError(t, depinject.Inject(opt)) // module registration failures: - appmodule.Register(&testpb.TestNoModuleOptionModule{}) + appconfig.RegisterModule(&testpb.TestNoModuleOptionModule{}) opt = appconfig.LoadYAML([]byte(` modules: - name: a @@ -112,7 +111,7 @@ modules: expectContainerErrorContains(t, opt, "module should have the option cosmos.app.v1alpha1.module") internal.ModuleRegistry = map[reflect.Type]*internal.ModuleInitializer{} // reset module registry - appmodule.Register(&testpb.TestNoGoImportModule{}) + appconfig.RegisterModule(&testpb.TestNoGoImportModule{}) opt = appconfig.LoadYAML([]byte(` modules: - name: a @@ -127,16 +126,16 @@ modules: // func init() { - appmodule.Register(&testpb.TestRuntimeModule{}, - appmodule.Provide(ProvideRuntimeState, ProvideStoreKey, ProvideApp), + appconfig.RegisterModule(&testpb.TestRuntimeModule{}, + appconfig.Provide(ProvideRuntimeState, ProvideStoreKey, ProvideApp), ) - appmodule.Register(&testpb.TestModuleA{}, - appmodule.Provide(ProvideModuleA), + appconfig.RegisterModule(&testpb.TestModuleA{}, + appconfig.Provide(ProvideModuleA), ) - appmodule.Register(&testpb.TestModuleB{}, - appmodule.Provide(ProvideModuleB), + appconfig.RegisterModule(&testpb.TestModuleB{}, + appconfig.Provide(ProvideModuleB), ) } diff --git a/depinject/appmodule/option.go b/depinject/appconfig/module.go similarity index 51% rename from depinject/appmodule/option.go rename to depinject/appconfig/module.go index df8f0c499469..fc4f62cbd525 100644 --- a/depinject/appmodule/option.go +++ b/depinject/appconfig/module.go @@ -1,9 +1,40 @@ -package appmodule +package appconfig import ( + "reflect" + + "google.golang.org/protobuf/proto" + internal "cosmossdk.io/depinject/internal/appconfig" ) +var Register = RegisterModule + +// Register registers a module with the global module registry. The provided +// protobuf message is used only to uniquely identify the protobuf module config +// type. The instance of the protobuf message used in the actual configuration +// will be injected into the container and can be requested by a provider +// function. All module initialization should be handled by the provided options. +// +// Protobuf message types used for module configuration should define the +// cosmos.app.v1alpha.module option and must explicitly specify go_package +// to make debugging easier for users. +func RegisterModule(msg proto.Message, options ...Option) { + ty := reflect.TypeOf(msg) + init := &internal.ModuleInitializer{ + ConfigProtoMessage: msg, + ConfigGoType: ty, + } + internal.ModuleRegistry[ty] = init + + for _, option := range options { + init.Error = option.apply(init) + if init.Error != nil { + return + } + } +} + // Option is a functional option for implementing modules. type Option interface { apply(*internal.ModuleInitializer) error @@ -16,7 +47,7 @@ func (f funcOption) apply(initializer *internal.ModuleInitializer) error { } // Provide registers providers with the dependency injection system that will be -// run within the module scope. See cosmossdk.io/depinject for +// run within the module scope (depinject.ProvideInModule). See cosmossdk.io/depinject for // documentation on the dependency injection system. func Provide(providers ...interface{}) Option { return funcOption(func(initializer *internal.ModuleInitializer) error { @@ -25,7 +56,7 @@ func Provide(providers ...interface{}) Option { }) } -// Invoke registers invokers to run with depinject. Each invoker will be called +// Invoke registers invokers to run with depinject (depinject.InvokeInModule). Each invoker will be called // at the end of dependency graph configuration in the order in which it was defined. Invokers may not define output // parameters, although they may return an error, and all of their input parameters will be marked as optional so that // invokers impose no additional constraints on the dependency graph. Invoker functions should nil-check all inputs. diff --git a/depinject/appmodule/register.go b/depinject/appmodule/register.go deleted file mode 100644 index ed68ebfcefef..000000000000 --- a/depinject/appmodule/register.go +++ /dev/null @@ -1,34 +0,0 @@ -package appmodule - -import ( - "reflect" - - "google.golang.org/protobuf/proto" - - internal "cosmossdk.io/depinject/internal/appconfig" -) - -// Register registers a module with the global module registry. The provided -// protobuf message is used only to uniquely identify the protobuf module config -// type. The instance of the protobuf message used in the actual configuration -// will be injected into the container and can be requested by a provider -// function. All module initialization should be handled by the provided options. -// -// Protobuf message types used for module configuration should define the -// cosmos.app.v1alpha.module option and must explicitly specify go_package -// to make debugging easier for users. -func Register(msg proto.Message, options ...Option) { - ty := reflect.TypeOf(msg) - init := &internal.ModuleInitializer{ - ConfigProtoMessage: msg, - ConfigGoType: ty, - } - internal.ModuleRegistry[ty] = init - - for _, option := range options { - init.Error = option.apply(init) - if init.Error != nil { - return - } - } -} diff --git a/depinject/internal/appconfig/buf.gen.yaml b/depinject/internal/appconfig/buf.gen.yaml index f4e31b03e8ae..760df9e925fa 100644 --- a/depinject/internal/appconfig/buf.gen.yaml +++ b/depinject/internal/appconfig/buf.gen.yaml @@ -2,7 +2,7 @@ version: v1 managed: enabled: true go_package_prefix: - default: cosmossdk.io/core/internal + default: cosmossdk.io/depinject/internal override: buf.build/cosmos/cosmos-sdk: cosmossdk.io/api plugins: diff --git a/depinject/internal/appconfig/registry.go b/depinject/internal/appconfig/registry.go index ce8dbb203f17..c79a50b4d29a 100644 --- a/depinject/internal/appconfig/registry.go +++ b/depinject/internal/appconfig/registry.go @@ -33,7 +33,7 @@ func ModulesByProtoMessageName() (map[protoreflect.FullName]*ModuleInitializer, descriptor := initializer.ConfigProtoMessage.ProtoReflect().Descriptor() fullName := descriptor.FullName() if _, ok := res[fullName]; ok { - return nil, fmt.Errorf("duplicate module registratio for %s", fullName) + return nil, fmt.Errorf("duplicate module registration for %s", fullName) } modDesc := proto.GetExtension(descriptor.Options(), appv1alpha1.E_Module).(*appv1alpha1.ModuleDescriptor) diff --git a/docs/architecture/adr-057-app-wiring.md b/docs/architecture/adr-057-app-wiring.md index ba1774c43125..239e7d52b352 100644 --- a/docs/architecture/adr-057-app-wiring.md +++ b/docs/architecture/adr-057-app-wiring.md @@ -4,6 +4,7 @@ * 2022-05-04: Initial Draft * 2022-08-19: Updates +* 2024-01-12: Updates ## Status @@ -79,7 +80,6 @@ of protobuf and its basic structure is very simple: ```protobuf package cosmos.app.v1; - message Config { repeated ModuleConfig modules = 1; } @@ -90,9 +90,7 @@ message ModuleConfig { } ``` -(See also https://github.com/cosmos/cosmos-sdk/blob/6e18f582bf69e3926a1e22a6de3c35ea327aadce/proto/cosmos/app/v1alpha1/config.proto) - -The configuration for every module is itself a protobuf message and modules will be identified and loaded based +The configuration for every module is a protobuf message and modules will be identified and loaded based on the protobuf type URL of their config object (ex. `cosmos.bank.module.v1.Module`). Modules are given a unique short `name` to share resources across different versions of the same module which might have a different protobuf package versions (ex. `cosmos.bank.module.v2.Module`). All module config objects should define the `cosmos.app.v1alpha1.module` @@ -181,13 +179,13 @@ Ex: ```go func init() { - appmodule.Register("cosmos.bank.module.v1.Module", - appmodule.Types( + appconfig.Register("cosmos.bank.module.v1.Module", + appconfig.Types( types.Types_tx_proto, types.Types_query_proto, types.Types_types_proto, ), - appmodule.Provide( + appconfig.Provide( provideBankModule, ) ) @@ -250,8 +248,6 @@ defined here are described in [ADR 063: Core Module API](./adr-063-core-module-a ### Registration of Inter-Module Hooks -### Registration of Inter-Module Hooks - Some modules define a hooks interface (ex. `StakingHooks`) which allows one module to call back into another module when certain events happen. @@ -260,9 +256,9 @@ which consumes these hooks can collect these hooks as a map of module name to ho ```go func init() { - appmodule.Register( + appconfig.RegisterModule( &foomodulev1.Module{}, - appmodule.Invoke(InvokeSetFooHooks), + appconfig.Invoke(InvokeSetFooHooks), ... ) } @@ -291,6 +287,8 @@ With the approach proposed here, hooks registration will be obviously observable ### Code Generation +> Not yet implemented + The `depinject` framework will optionally allow the app configuration and dependency injection wiring to be code generated. This will allow: @@ -303,15 +301,17 @@ Code generation requires that all providers and invokers and their parameters ar When we start creating semantically versioned SDK modules that are in standalone go modules, a state machine breaking change to a module should be handled as follows: -- the semantic major version should be incremented, and -- a new semantically versioned module config protobuf type should be created. + +* the semantic major version should be incremented, and +* a new semantically versioned module config protobuf type should be created. For instance, if we have the SDK module for bank in the go module `cosmossdk.io/x/bank` with the module config type `cosmos.bank.module.v1.Module`, and we want to make a state machine breaking change to the module, we would: -- create a new go module `cosmossdk.io/x/bank/v2`, -- with the module config protobuf type `cosmos.bank.module.v2.Module`. -This _does not_ mean that we need to increment the protobuf API version for bank. Both modules can support +* create a new go module `cosmossdk.io/x/bank/v2`, +* with the module config protobuf type `cosmos.bank.module.v2.Module`. + +This *does not* mean that we need to increment the protobuf API version for bank. Both modules can support `cosmos.bank.v1`, but `cosmossdk.io/x/bank/v2` will be a separate go module with a separate module config type. This practice will eventually allow us to use appconfig to load new versions of a module via a configuration change. @@ -320,7 +320,7 @@ Effectively, there should be a 1:1 correspondence between a semantically version versioned module config protobuf type, and major versioning bumps should occur whenever state machine breaking changes are made to a module. -NOTE: SDK modules that are standalone go modules _should not_ adopt semantic versioning until the concerns described in +NOTE: SDK modules that are standalone go modules *should not* adopt semantic versioning until the concerns described in [ADR 054: Module Semantic Versioning](./adr-054-semver-compatible-modules.md) are addressed. The short-term solution for this issue was left somewhat unresolved. However, the easiest tactic is likely to use a standalone API go module and follow the guidelines described in this comment: https://github.com/cosmos/cosmos-sdk/pull/11802#issuecomment-1406815181. For the time-being, it is recommended that diff --git a/docs/build/building-modules/11-structure.md b/docs/build/building-modules/11-structure.md index d8f4027d3350..9a40d4e55393 100644 --- a/docs/build/building-modules/11-structure.md +++ b/docs/build/building-modules/11-structure.md @@ -54,6 +54,7 @@ x/{module_name} │   └── module.go │   └── abci.go │   └── autocli.go +│   └── depinject.go ├── simulation │   ├── decoder.go │   ├── genesis.go diff --git a/go.mod b/go.mod index 76010ef9a948..764e80ae8b1b 100644 --- a/go.mod +++ b/go.mod @@ -178,6 +178,7 @@ require ( // ) // TODO remove after all modules have their own go.mods replace ( + cosmossdk.io/api => ./api cosmossdk.io/depinject => ./depinject cosmossdk.io/x/auth => ./x/auth cosmossdk.io/x/bank => ./x/bank diff --git a/go.sum b/go.sum index 1d78a39ebbd9..c7a0cbd54eea 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a h1:Zr++x1RCJWi+K8bTZsQKdjtL4SzyHBLGM3Fcn75iWf0= -cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a/go.mod h1:7B/5XWh1HYwJk3DzWeNoxOSI+nGx1m5UyYfHLFuKzkw= cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 h1:hOzi4yo2Fc7h3mod+xX4m4QA4+Uq+PkFRjY/yalZ0B8= diff --git a/orm/model/ormdb/module_test.go b/orm/model/ormdb/module_test.go index a146aa78b825..18c7f558782e 100644 --- a/orm/model/ormdb/module_test.go +++ b/orm/model/ormdb/module_test.go @@ -13,14 +13,12 @@ import ( "gotest.tools/v3/assert" "gotest.tools/v3/golden" - appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" ormmodulev1alpha1 "cosmossdk.io/api/cosmos/orm/module/v1alpha1" ormv1alpha1 "cosmossdk.io/api/cosmos/orm/v1alpha1" "cosmossdk.io/core/genesis" "cosmossdk.io/core/store" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" - am "cosmossdk.io/depinject/appmodule" _ "cosmossdk.io/orm" // required for ORM module registration "cosmossdk.io/orm/internal/testkv" "cosmossdk.io/orm/internal/testpb" diff --git a/orm/orm.go b/orm/orm.go index c9d7872508b3..cd292e18da29 100644 --- a/orm/orm.go +++ b/orm/orm.go @@ -12,14 +12,14 @@ import ( ormv1alpha1 "cosmossdk.io/api/cosmos/orm/v1alpha1" "cosmossdk.io/core/store" "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" + "cosmossdk.io/depinject/appconfig" "cosmossdk.io/orm/model/ormdb" "cosmossdk.io/orm/model/ormtable" ) func init() { - am.Register(&modulev1alpha1.Module{}, - am.Provide(ProvideModuleDB), + appconfig.RegisterModule(&modulev1alpha1.Module{}, + appconfig.Provide(ProvideModuleDB), ) } diff --git a/proto/cosmos/app/v1alpha1/query.proto b/proto/cosmos/app/v1alpha1/query.proto index efec9c81ad71..2c222d870647 100644 --- a/proto/cosmos/app/v1alpha1/query.proto +++ b/proto/cosmos/app/v1alpha1/query.proto @@ -8,7 +8,9 @@ import "cosmos/app/v1alpha1/config.proto"; service Query { // Config returns the current app config. - rpc Config(QueryConfigRequest) returns (QueryConfigResponse) {} + rpc Config(QueryConfigRequest) returns (QueryConfigResponse) { + option deprecated = true; + } } // QueryConfigRequest is the Query/Config request type. diff --git a/runtime/autocli.go b/runtime/autocli.go index 49ff9619115f..a942a3bee2fa 100644 --- a/runtime/autocli.go +++ b/runtime/autocli.go @@ -1,7 +1,6 @@ package runtime import ( - appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1" ) @@ -9,13 +8,6 @@ import ( func (m appModule) AutoCLIOptions() *autocliv1.ModuleOptions { return &autocliv1.ModuleOptions{ Query: &autocliv1.ServiceCommandDescriptor{ - Service: appv1alpha1.Query_ServiceDesc.ServiceName, - RpcCommandOptions: []*autocliv1.RpcCommandOptions{ - { - RpcMethod: "Config", - Short: "Query the current app config", - }, - }, SubCommands: map[string]*autocliv1.ServiceCommandDescriptor{ "autocli": { Service: autocliv1.Query_ServiceDesc.ServiceName, diff --git a/runtime/module.go b/runtime/module.go index 0c4cbda7da7a..888bdef52c5e 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -18,7 +18,7 @@ import ( "cosmossdk.io/core/genesis" "cosmossdk.io/core/store" "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" + "cosmossdk.io/depinject/appconfig" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/tx/signing" @@ -59,8 +59,8 @@ type BaseAppOption func(*baseapp.BaseApp) func (b BaseAppOption) IsManyPerContainerType() {} func init() { - am.Register(&runtimev1alpha1.Module{}, - am.Provide( + appconfig.RegisterModule(&runtimev1alpha1.Module{}, + appconfig.Provide( ProvideApp, ProvideInterfaceRegistry, ProvideKVStoreKey, @@ -75,7 +75,7 @@ func init() { ProvideAppVersionModifier, ProvideAddressCodec, ), - am.Invoke(SetupAppBuilder), + appconfig.Invoke(SetupAppBuilder), ) } diff --git a/runtime/services.go b/runtime/services.go index 0b9223d92a29..e73ed94f3819 100644 --- a/runtime/services.go +++ b/runtime/services.go @@ -1,7 +1,6 @@ package runtime import ( - appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1" @@ -10,7 +9,6 @@ import ( ) func (a *App) registerRuntimeServices(cfg module.Configurator) error { - appv1alpha1.RegisterQueryServer(cfg.QueryServer(), services.NewAppQueryService(a.appConfig)) autocliv1.RegisterQueryServer(cfg.QueryServer(), services.NewAutoCLIQueryService(a.ModuleManager.Modules)) reflectionSvc, err := services.NewReflectionService() diff --git a/runtime/services/app.go b/runtime/services/app.go deleted file mode 100644 index fa80a281025e..000000000000 --- a/runtime/services/app.go +++ /dev/null @@ -1,23 +0,0 @@ -package services - -import ( - "context" - - appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" -) - -// AppQueryService implements the cosmos.app.v1alpha1.Query service -type AppQueryService struct { - appv1alpha1.UnimplementedQueryServer - appConfig *appv1alpha1.Config -} - -func NewAppQueryService(appConfig *appv1alpha1.Config) *AppQueryService { - return &AppQueryService{appConfig: appConfig} -} - -func (a *AppQueryService) Config(context.Context, *appv1alpha1.QueryConfigRequest) (*appv1alpha1.QueryConfigResponse, error) { - return &appv1alpha1.QueryConfigResponse{Config: a.appConfig}, nil -} - -var _ appv1alpha1.QueryServer = &AppQueryService{} diff --git a/simapp/app_config.go b/simapp/app_config.go index ce7cb08bae7f..d0ac7d7fbc16 100644 --- a/simapp/app_config.go +++ b/simapp/app_config.go @@ -26,7 +26,6 @@ import ( txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1" upgrademodulev1 "cosmossdk.io/api/cosmos/upgrade/module/v1" vestingmodulev1 "cosmossdk.io/api/cosmos/vesting/module/v1" - "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" _ "cosmossdk.io/x/auth/tx/config" // import for side-effects authtypes "cosmossdk.io/x/auth/types" @@ -44,8 +43,6 @@ import ( evidencetypes "cosmossdk.io/x/evidence/types" "cosmossdk.io/x/feegrant" _ "cosmossdk.io/x/feegrant/module" // import for side-effects - "cosmossdk.io/x/gov" - govclient "cosmossdk.io/x/gov/client" govtypes "cosmossdk.io/x/gov/types" "cosmossdk.io/x/group" _ "cosmossdk.io/x/group/module" // import for side-effects @@ -63,10 +60,8 @@ import ( upgradetypes "cosmossdk.io/x/upgrade/types" "github.com/cosmos/cosmos-sdk/runtime" - "github.com/cosmos/cosmos-sdk/types/module" _ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side-effects consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types" - "github.com/cosmos/cosmos-sdk/x/genutil" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) @@ -97,7 +92,7 @@ var ( } // application configuration (used by depinject) - AppConfig = depinject.Configs(appconfig.Compose(&appv1alpha1.Config{ + appConfig = appconfig.Compose(&appv1alpha1.Config{ Modules: []*appv1alpha1.ModuleConfig{ { Name: runtime.ModuleName, @@ -254,14 +249,5 @@ var ( Config: appconfig.WrapAny(&poolmodulev1.Module{}), }, }, - }), - depinject.Supply( - // supply custom module basics - map[string]module.AppModuleBasic{ - genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator), - govtypes.ModuleName: gov.NewAppModuleBasic( - []govclient.ProposalHandler{}, - ), - }, - )) + }) ) diff --git a/simapp/app_v2.go b/simapp/app_v2.go index 0c4ea6f63ac8..3b42c12c1043 100644 --- a/simapp/app_v2.go +++ b/simapp/app_v2.go @@ -3,6 +3,7 @@ package simapp import ( + _ "embed" "fmt" "io" "os" @@ -25,7 +26,10 @@ import ( distrkeeper "cosmossdk.io/x/distribution/keeper" evidencekeeper "cosmossdk.io/x/evidence/keeper" feegrantkeeper "cosmossdk.io/x/feegrant/keeper" + "cosmossdk.io/x/gov" + govclient "cosmossdk.io/x/gov/client" govkeeper "cosmossdk.io/x/gov/keeper" + govtypes "cosmossdk.io/x/gov/types" groupkeeper "cosmossdk.io/x/group/keeper" mintkeeper "cosmossdk.io/x/mint/keeper" nftkeeper "cosmossdk.io/x/nft/keeper" @@ -48,6 +52,8 @@ import ( testdata_pulsar "github.com/cosmos/cosmos-sdk/testutil/testdata/testpb" "github.com/cosmos/cosmos-sdk/types/module" consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper" + "github.com/cosmos/cosmos-sdk/x/genutil" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) // DefaultNodeHome default home directories for the application daemon @@ -101,6 +107,23 @@ func init() { DefaultNodeHome = filepath.Join(userHomeDir, ".simapp") } +// AppConfig returns the default app config. +func AppConfig() depinject.Config { + return depinject.Configs( + // appconfig.LoadYAML(AppConfigYAML), + appConfig, + depinject.Supply( + // supply custom module basics + map[string]module.AppModuleBasic{ + genutiltypes.ModuleName: genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator), + govtypes.ModuleName: gov.NewAppModuleBasic( + []govclient.ProposalHandler{}, + ), + }, + ), + ) +} + // NewSimApp returns a reference to an initialized SimApp. func NewSimApp( logger log.Logger, @@ -116,7 +139,7 @@ func NewSimApp( // merge the AppConfig and other configuration in one config appConfig = depinject.Configs( - AppConfig, + AppConfig(), depinject.Supply( // supply the application options appOpts, diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index de8bcc17cac2..48f5039ab9bf 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -37,7 +37,7 @@ func NewRootCmd() *cobra.Command { ) if err := depinject.Inject( - depinject.Configs(simapp.AppConfig, + depinject.Configs(simapp.AppConfig(), depinject.Supply( log.NewNopLogger(), simtestutil.NewAppOptionsWithFlagHome(tempDir()), diff --git a/tests/integration/runtime/query_test.go b/tests/integration/runtime/query_test.go index f018c6ac1999..4ad7a7553aed 100644 --- a/tests/integration/runtime/query_test.go +++ b/tests/integration/runtime/query_test.go @@ -66,28 +66,6 @@ func initFixture(t assert.TestingT) *fixture { return f } -func TestQueryAppConfig(t *testing.T) { - t.Parallel() - f := initFixture(t) - - res, err := f.appQueryClient.Config(f.ctx, &appv1alpha1.QueryConfigRequest{}) - assert.NilError(t, err) - // app config is not nil - assert.Assert(t, res != nil && res.Config != nil) - - moduleConfigs := map[string]*appv1alpha1.ModuleConfig{} - for _, module := range res.Config.Modules { - moduleConfigs[module.Name] = module - } - - // has all expected modules - for _, modName := range []string{"auth", "bank", "tx", "consensus", "runtime", "staking"} { - modConfig := moduleConfigs[modName] - assert.Assert(t, modConfig != nil) - assert.Assert(t, modConfig.Config != nil) - } -} - func TestReflectionService(t *testing.T) { t.Parallel() f := initFixture(t) diff --git a/x/accounts/depinject.go b/x/accounts/depinject.go new file mode 100644 index 000000000000..bef3dd4208cf --- /dev/null +++ b/x/accounts/depinject.go @@ -0,0 +1,8 @@ +package accounts + +import "cosmossdk.io/depinject" + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} diff --git a/x/accounts/go.mod b/x/accounts/go.mod index 44bd0f975256..35c8611b30f8 100644 --- a/x/accounts/go.mod +++ b/x/accounts/go.mod @@ -6,6 +6,7 @@ require ( cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a cosmossdk.io/collections v0.4.0 cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 + cosmossdk.io/depinject v1.0.0-alpha.4 github.com/cosmos/cosmos-sdk v0.51.0 github.com/cosmos/gogoproto v1.4.11 github.com/grpc-ecosystem/grpc-gateway v1.16.0 @@ -17,7 +18,6 @@ require ( ) require ( - cosmossdk.io/depinject v1.0.0-alpha.4 // indirect cosmossdk.io/errors v1.0.1 // indirect cosmossdk.io/log v1.3.0 // indirect cosmossdk.io/math v1.2.0 // indirect diff --git a/x/accounts/module.go b/x/accounts/module.go index 4d00f15ed345..e3d55155760f 100644 --- a/x/accounts/module.go +++ b/x/accounts/module.go @@ -47,8 +47,6 @@ type AppModule struct { k Keeper } -func (m AppModule) IsOnePerModuleType() {} - func (m AppModule) IsAppModule() {} func (m AppModule) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {} diff --git a/x/auth/depinject.go b/x/auth/depinject.go new file mode 100644 index 000000000000..dcd624f99d10 --- /dev/null +++ b/x/auth/depinject.go @@ -0,0 +1,77 @@ +package auth + +import ( + modulev1 "cosmossdk.io/api/cosmos/auth/module/v1" + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/auth/keeper" + "cosmossdk.io/x/auth/simulation" + "cosmossdk.io/x/auth/types" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + + AddressCodec address.Codec + RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"` + AccountI func() sdk.AccountI `optional:"true"` +} + +type ModuleOutputs struct { + depinject.Out + + AccountKeeper keeper.AccountKeeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + maccPerms := map[string][]string{} + for _, permission := range in.Config.ModuleAccountPermissions { + maccPerms[permission.Account] = permission.Permissions + } + + // default to governance authority if not provided + authority := types.NewModuleAddress(GovModuleName) + if in.Config.Authority != "" { + authority = types.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + if in.RandomGenesisAccountsFn == nil { + in.RandomGenesisAccountsFn = simulation.RandomGenesisAccounts + } + + if in.AccountI == nil { + in.AccountI = types.ProtoBaseAccount + } + + auth, err := in.AddressCodec.BytesToString(authority) + if err != nil { + panic(err) + } + + k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth) + m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn) + + return ModuleOutputs{AccountKeeper: k, Module: m} +} diff --git a/x/auth/module.go b/x/auth/module.go index 534bcd43e018..1421f8ffd652 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -7,12 +7,8 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/auth/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "cosmossdk.io/x/auth/keeper" "cosmossdk.io/x/auth/simulation" "cosmossdk.io/x/auth/types" @@ -20,7 +16,6 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) @@ -36,7 +31,6 @@ var ( _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} ) @@ -92,9 +86,6 @@ type AppModule struct { randGenAccountsFn types.RandomGenesisAccountsFn } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -169,63 +160,3 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { return nil } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - - AddressCodec address.Codec - RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"` - AccountI func() sdk.AccountI `optional:"true"` -} - -type ModuleOutputs struct { - depinject.Out - - AccountKeeper keeper.AccountKeeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - maccPerms := map[string][]string{} - for _, permission := range in.Config.ModuleAccountPermissions { - maccPerms[permission.Account] = permission.Permissions - } - - // default to governance authority if not provided - authority := types.NewModuleAddress(GovModuleName) - if in.Config.Authority != "" { - authority = types.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - if in.RandomGenesisAccountsFn == nil { - in.RandomGenesisAccountsFn = simulation.RandomGenesisAccounts - } - - if in.AccountI == nil { - in.AccountI = types.ProtoBaseAccount - } - - auth, err := in.AddressCodec.BytesToString(authority) - if err != nil { - panic(err) - } - - k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth) - m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn) - - return ModuleOutputs{AccountKeeper: k, Module: m} -} diff --git a/x/auth/tx/config/config.go b/x/auth/tx/config/depinject.go similarity index 97% rename from x/auth/tx/config/config.go rename to x/auth/tx/config/depinject.go index 30397e33a91e..dff980a5a790 100644 --- a/x/auth/tx/config/config.go +++ b/x/auth/tx/config/depinject.go @@ -12,7 +12,7 @@ import ( txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1" "cosmossdk.io/core/address" "cosmossdk.io/depinject" - "cosmossdk.io/depinject/appmodule" + "cosmossdk.io/depinject/appconfig" "cosmossdk.io/x/auth/ante" "cosmossdk.io/x/auth/posthandler" "cosmossdk.io/x/auth/tx" @@ -30,9 +30,9 @@ import ( ) func init() { - appmodule.Register(&txconfigv1.Config{}, - appmodule.Provide(ProvideModule), - appmodule.Provide(ProvideProtoRegistry), + appconfig.RegisterModule(&txconfigv1.Config{}, + appconfig.Provide(ProvideModule), + appconfig.Provide(ProvideProtoRegistry), ) } diff --git a/x/auth/vesting/depinject.go b/x/auth/vesting/depinject.go new file mode 100644 index 000000000000..c2f346f03207 --- /dev/null +++ b/x/auth/vesting/depinject.go @@ -0,0 +1,40 @@ +package vesting + +import ( + modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/auth/keeper" + "cosmossdk.io/x/auth/vesting/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + AccountKeeper keeper.AccountKeeper + BankKeeper types.BankKeeper +} + +type ModuleOutputs struct { + depinject.Out + + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + m := NewAppModule(in.AccountKeeper, in.BankKeeper) + + return ModuleOutputs{Module: m} +} diff --git a/x/auth/vesting/module.go b/x/auth/vesting/module.go index 085ef6138713..5075184d4192 100644 --- a/x/auth/vesting/module.go +++ b/x/auth/vesting/module.go @@ -8,10 +8,7 @@ import ( "github.com/spf13/cobra" "google.golang.org/grpc" - modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "cosmossdk.io/x/auth/keeper" "cosmossdk.io/x/auth/vesting/client/cli" "cosmossdk.io/x/auth/vesting/types" @@ -86,9 +83,6 @@ func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule { } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -108,32 +102,3 @@ func (am AppModule) ExportGenesis(_ context.Context, cdc codec.JSONCodec) json.R // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return 1 } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - AccountKeeper keeper.AccountKeeper - BankKeeper types.BankKeeper -} - -type ModuleOutputs struct { - depinject.Out - - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - m := NewAppModule(in.AccountKeeper, in.BankKeeper) - - return ModuleOutputs{Module: m} -} diff --git a/x/authz/module/abci.go b/x/authz/module/abci.go index 395508c7462f..5e1b3038c9d6 100644 --- a/x/authz/module/abci.go +++ b/x/authz/module/abci.go @@ -1,4 +1,4 @@ -package authz +package module import ( "context" diff --git a/x/authz/module/abci_test.go b/x/authz/module/abci_test.go index 13265d97bd63..f4a82573886a 100644 --- a/x/authz/module/abci_test.go +++ b/x/authz/module/abci_test.go @@ -1,4 +1,4 @@ -package authz_test +package module_test import ( "testing" diff --git a/x/authz/module/autocli.go b/x/authz/module/autocli.go index dbcf85c371f3..0ce9d4968e6d 100644 --- a/x/authz/module/autocli.go +++ b/x/authz/module/autocli.go @@ -1,4 +1,4 @@ -package authz +package module import ( "fmt" diff --git a/x/authz/module/depinject.go b/x/authz/module/depinject.go new file mode 100644 index 000000000000..29d5a347d428 --- /dev/null +++ b/x/authz/module/depinject.go @@ -0,0 +1,51 @@ +package module + +import ( + modulev1 "cosmossdk.io/api/cosmos/authz/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/authz" + "cosmossdk.io/x/authz/keeper" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Cdc codec.Codec + AccountKeeper authz.AccountKeeper + BankKeeper authz.BankKeeper + Registry cdctypes.InterfaceRegistry + MsgServiceRouter baseapp.MessageRouter + StoreService store.KVStoreService +} + +type ModuleOutputs struct { + depinject.Out + + AuthzKeeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + k := keeper.NewKeeper(in.StoreService, in.Cdc, in.MsgServiceRouter, in.AccountKeeper) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) + return ModuleOutputs{AuthzKeeper: k, Module: m} +} diff --git a/x/authz/module/module.go b/x/authz/module/module.go index 62156ebe8739..089b7e2c55dc 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -1,4 +1,4 @@ -package authz +package module import ( "context" @@ -8,18 +8,13 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/authz/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "cosmossdk.io/errors" "cosmossdk.io/x/authz" "cosmossdk.io/x/authz/client/cli" "cosmossdk.io/x/authz/keeper" "cosmossdk.io/x/authz/simulation" - "github.com/cosmos/cosmos-sdk/baseapp" sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -32,7 +27,6 @@ var ( _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} @@ -119,9 +113,6 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper, } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -148,39 +139,6 @@ func (am AppModule) BeginBlock(ctx context.Context) error { return BeginBlocker(ctx, am.keeper) } -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Cdc codec.Codec - AccountKeeper authz.AccountKeeper - BankKeeper authz.BankKeeper - Registry cdctypes.InterfaceRegistry - MsgServiceRouter baseapp.MessageRouter - StoreService store.KVStoreService -} - -type ModuleOutputs struct { - depinject.Out - - AuthzKeeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - k := keeper.NewKeeper(in.StoreService, in.Cdc, in.MsgServiceRouter, in.AccountKeeper) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) - return ModuleOutputs{AuthzKeeper: k, Module: m} -} - -// ____________________________________________________________________________ - // AppModuleSimulation functions // GenerateGenesisState creates a randomized GenState of the authz module. diff --git a/x/bank/depinject.go b/x/bank/depinject.go new file mode 100644 index 000000000000..ea805a4614b8 --- /dev/null +++ b/x/bank/depinject.go @@ -0,0 +1,92 @@ +package bank + +import ( + modulev1 "cosmossdk.io/api/cosmos/bank/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/log" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/bank/keeper" + "cosmossdk.io/x/bank/types" + + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + Cdc codec.Codec + StoreService store.KVStoreService + Logger log.Logger + + AccountKeeper types.AccountKeeper +} + +type ModuleOutputs struct { + depinject.Out + + BankKeeper keeper.BaseKeeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // Configure blocked module accounts. + // + // Default behavior for blockedAddresses is to regard any module mentioned in + // AccountKeeper's module account permissions as blocked. + blockedAddresses := make(map[string]bool) + if len(in.Config.BlockedModuleAccountsOverride) > 0 { + for _, moduleName := range in.Config.BlockedModuleAccountsOverride { + addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(moduleName)) + if err != nil { + panic(err) + } + blockedAddresses[addrStr] = true + } + } else { + for _, permission := range in.AccountKeeper.GetModulePermissions() { + addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(permission.GetAddress()) + if err != nil { + panic(err) + } + blockedAddresses[addrStr] = true + } + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + + bankKeeper := keeper.NewBaseKeeper( + in.Cdc, + in.StoreService, + in.AccountKeeper, + blockedAddresses, + authStr, + in.Logger, + ) + m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper) + + return ModuleOutputs{BankKeeper: bankKeeper, Module: m} +} diff --git a/x/bank/module.go b/x/bank/module.go index 1d0c56ee76bb..3b5541e0101f 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -9,13 +9,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/bank/module/v1" "cosmossdk.io/core/appmodule" - corestore "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - "cosmossdk.io/log" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/bank/client/cli" "cosmossdk.io/x/bank/keeper" "cosmossdk.io/x/bank/simulation" @@ -39,7 +33,6 @@ var ( _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} ) @@ -98,9 +91,6 @@ type AppModule struct { accountKeeper types.AccountKeeper } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -185,77 +175,3 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp simState.AppParams, simState.Cdc, simState.TxConfig, am.accountKeeper, am.keeper, ) } - -// App Wiring Setup - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - Cdc codec.Codec - StoreService corestore.KVStoreService - Logger log.Logger - - AccountKeeper types.AccountKeeper -} - -type ModuleOutputs struct { - depinject.Out - - BankKeeper keeper.BaseKeeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // Configure blocked module accounts. - // - // Default behavior for blockedAddresses is to regard any module mentioned in - // AccountKeeper's module account permissions as blocked. - blockedAddresses := make(map[string]bool) - if len(in.Config.BlockedModuleAccountsOverride) > 0 { - for _, moduleName := range in.Config.BlockedModuleAccountsOverride { - addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(moduleName)) - if err != nil { - panic(err) - } - blockedAddresses[addrStr] = true - } - } else { - for _, permission := range in.AccountKeeper.GetModulePermissions() { - addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(permission.GetAddress()) - if err != nil { - panic(err) - } - blockedAddresses[addrStr] = true - } - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) - if err != nil { - panic(err) - } - - bankKeeper := keeper.NewBaseKeeper( - in.Cdc, - in.StoreService, - in.AccountKeeper, - blockedAddresses, - authStr, - in.Logger, - ) - m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper) - - return ModuleOutputs{BankKeeper: bankKeeper, Module: m} -} diff --git a/x/circuit/depinject.go b/x/circuit/depinject.go new file mode 100644 index 000000000000..fdca2207413c --- /dev/null +++ b/x/circuit/depinject.go @@ -0,0 +1,68 @@ +package circuit + +import ( + modulev1 "cosmossdk.io/api/cosmos/circuit/module/v1" + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/circuit/keeper" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + Cdc codec.Codec + StoreService store.KVStoreService + + AddressCodec address.Codec +} + +type ModuleOutputs struct { + depinject.Out + + CircuitKeeper keeper.Keeper + Module appmodule.AppModule + BaseappOptions runtime.BaseAppOption +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress("gov") + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + circuitkeeper := keeper.NewKeeper( + in.Cdc, + in.StoreService, + authority.String(), + in.AddressCodec, + ) + m := NewAppModule(in.Cdc, circuitkeeper) + + baseappOpt := func(app *baseapp.BaseApp) { + app.SetCircuitBreaker(&circuitkeeper) + } + + return ModuleOutputs{CircuitKeeper: circuitkeeper, Module: m, BaseappOptions: baseappOpt} +} diff --git a/x/circuit/module.go b/x/circuit/module.go index 667a1fd9700a..b2e61cb19548 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -8,21 +8,13 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/circuit/module/v1" - "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/circuit/keeper" "cosmossdk.io/x/circuit/types" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/telemetry" "github.com/cosmos/cosmos-sdk/types/module" ) @@ -85,9 +77,6 @@ type AppModule struct { keeper keeper.Keeper } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -125,50 +114,3 @@ func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - Cdc codec.Codec - StoreService store.KVStoreService - - AddressCodec address.Codec -} - -type ModuleOutputs struct { - depinject.Out - - CircuitKeeper keeper.Keeper - Module appmodule.AppModule - BaseappOptions runtime.BaseAppOption -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress("gov") - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - circuitkeeper := keeper.NewKeeper( - in.Cdc, - in.StoreService, - authority.String(), - in.AddressCodec, - ) - m := NewAppModule(in.Cdc, circuitkeeper) - - baseappOpt := func(app *baseapp.BaseApp) { - app.SetCircuitBreaker(&circuitkeeper) - } - - return ModuleOutputs{CircuitKeeper: circuitkeeper, Module: m, BaseappOptions: baseappOpt} -} diff --git a/x/consensus/depinject.go b/x/consensus/depinject.go new file mode 100644 index 000000000000..9f613acf3649 --- /dev/null +++ b/x/consensus/depinject.go @@ -0,0 +1,65 @@ +package consensus + +import ( + modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" + storetypes "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/x/consensus/keeper" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + Cdc codec.Codec + StoreService storetypes.KVStoreService + EventManager event.Service +} + +type ModuleOutputs struct { + depinject.Out + + Keeper keeper.Keeper + Module appmodule.AppModule + BaseAppOption runtime.BaseAppOption +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress("gov") + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String(), in.EventManager) + m := NewAppModule(in.Cdc, k) + baseappOpt := func(app *baseapp.BaseApp) { + app.SetParamStore(k.ParamsStore) + } + + return ModuleOutputs{ + Keeper: k, + Module: m, + BaseAppOption: baseappOpt, + } +} diff --git a/x/consensus/module.go b/x/consensus/module.go index 03c5edc1106a..a0fcc9b161fe 100644 --- a/x/consensus/module.go +++ b/x/consensus/module.go @@ -6,19 +6,11 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" - modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/event" - storetypes "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/consensus/keeper" "github.com/cosmos/cosmos-sdk/x/consensus/types" @@ -65,9 +57,6 @@ type AppModule struct { keeper keeper.Keeper } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -88,47 +77,3 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule { // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - Cdc codec.Codec - StoreService storetypes.KVStoreService - EventManager event.Service -} - -type ModuleOutputs struct { - depinject.Out - - Keeper keeper.Keeper - Module appmodule.AppModule - BaseAppOption runtime.BaseAppOption -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress("gov") - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String(), in.EventManager) - m := NewAppModule(in.Cdc, k) - baseappOpt := func(app *baseapp.BaseApp) { - app.SetParamStore(k.ParamsStore) - } - - return ModuleOutputs{ - Keeper: k, - Module: m, - BaseAppOption: baseappOpt, - } -} diff --git a/x/counter/depinject.go b/x/counter/depinject.go new file mode 100644 index 000000000000..a1297170602b --- /dev/null +++ b/x/counter/depinject.go @@ -0,0 +1,49 @@ +package counter + +import ( + modulev1 "cosmossdk.io/api/cosmos/counter/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" + storetypes "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + + "github.com/cosmos/cosmos-sdk/x/counter/keeper" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService storetypes.KVStoreService + EventManager event.Service +} + +type ModuleOutputs struct { + depinject.Out + + Keeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + k := keeper.NewKeeper(in.StoreService, in.EventManager) + m := NewAppModule(k) + + return ModuleOutputs{ + Keeper: k, + Module: m, + } +} diff --git a/x/counter/module.go b/x/counter/module.go index fae00c214081..1a4a97befe8b 100644 --- a/x/counter/module.go +++ b/x/counter/module.go @@ -4,12 +4,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" - modulev1 "cosmossdk.io/api/cosmos/counter/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/event" - storetypes "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -49,9 +44,6 @@ type AppModule struct { keeper keeper.Keeper } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -72,35 +64,3 @@ func NewAppModule(keeper keeper.Keeper) AppModule { // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return 1 } - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService storetypes.KVStoreService - EventManager event.Service -} - -type ModuleOutputs struct { - depinject.Out - - Keeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - k := keeper.NewKeeper(in.StoreService, in.EventManager) - m := NewAppModule(k) - - return ModuleOutputs{ - Keeper: k, - Module: m, - } -} diff --git a/x/crisis/depinject.go b/x/crisis/depinject.go new file mode 100644 index 000000000000..4bbbd3417daa --- /dev/null +++ b/x/crisis/depinject.go @@ -0,0 +1,87 @@ +package crisis + +import ( + "github.com/spf13/cast" + + modulev1 "cosmossdk.io/api/cosmos/crisis/module/v1" + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server" + servertypes "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/x/crisis/keeper" + "github.com/cosmos/cosmos-sdk/x/crisis/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService store.KVStoreService + Codec codec.Codec + AppOpts servertypes.AppOptions `optional:"true"` + + BankKeeper types.SupplyKeeper + AddressCodec address.Codec +} + +type ModuleOutputs struct { + depinject.Out + + Module appmodule.AppModule + CrisisKeeper *keeper.Keeper +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + var invalidCheckPeriod uint + if in.AppOpts != nil { + invalidCheckPeriod = cast.ToUint(in.AppOpts.Get(server.FlagInvCheckPeriod)) + } + + feeCollectorName := in.Config.FeeCollectorName + if feeCollectorName == "" { + feeCollectorName = authtypes.FeeCollectorName + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + k := keeper.NewKeeper( + in.Codec, + in.StoreService, + invalidCheckPeriod, + in.BankKeeper, + feeCollectorName, + authority.String(), + in.AddressCodec, + ) + + var skipGenesisInvariants bool + if in.AppOpts != nil { + skipGenesisInvariants = cast.ToBool(in.AppOpts.Get(FlagSkipGenesisInvariants)) + } + + m := NewAppModule(k, skipGenesisInvariants) + + return ModuleOutputs{CrisisKeeper: k, Module: m} +} diff --git a/x/crisis/module.go b/x/crisis/module.go index 6fee91a29ffb..a82b9ab3ff4d 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -7,22 +7,13 @@ import ( "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/spf13/cast" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/crisis/module/v1" - "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/server" - servertypes "github.com/cosmos/cosmos-sdk/server/types" "github.com/cosmos/cosmos-sdk/telemetry" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/crisis/keeper" @@ -109,9 +100,6 @@ func NewAppModule(keeper *keeper.Keeper, skipGenesisInvariants bool) AppModule { } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -160,68 +148,3 @@ func (am AppModule) EndBlock(ctx context.Context) error { EndBlocker(ctx, *am.keeper) return nil } - -// App Wiring Setup - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - AppOpts servertypes.AppOptions `optional:"true"` - - BankKeeper types.SupplyKeeper - AddressCodec address.Codec -} - -type ModuleOutputs struct { - depinject.Out - - Module appmodule.AppModule - CrisisKeeper *keeper.Keeper -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - var invalidCheckPeriod uint - if in.AppOpts != nil { - invalidCheckPeriod = cast.ToUint(in.AppOpts.Get(server.FlagInvCheckPeriod)) - } - - feeCollectorName := in.Config.FeeCollectorName - if feeCollectorName == "" { - feeCollectorName = authtypes.FeeCollectorName - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - k := keeper.NewKeeper( - in.Cdc, - in.StoreService, - invalidCheckPeriod, - in.BankKeeper, - feeCollectorName, - authority.String(), - in.AddressCodec, - ) - - var skipGenesisInvariants bool - if in.AppOpts != nil { - skipGenesisInvariants = cast.ToBool(in.AppOpts.Get(FlagSkipGenesisInvariants)) - } - - m := NewAppModule(k, skipGenesisInvariants) - - return ModuleOutputs{CrisisKeeper: k, Module: m} -} diff --git a/x/distribution/depinject.go b/x/distribution/depinject.go new file mode 100644 index 000000000000..78b0fea3a2e4 --- /dev/null +++ b/x/distribution/depinject.go @@ -0,0 +1,79 @@ +package distribution + +import ( + modulev1 "cosmossdk.io/api/cosmos/distribution/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/distribution/keeper" + "cosmossdk.io/x/distribution/types" + staking "cosmossdk.io/x/staking/types" + + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + StakingKeeper types.StakingKeeper + PoolKeeper types.PoolKeeper +} + +type ModuleOutputs struct { + depinject.Out + + DistrKeeper keeper.Keeper + Module appmodule.AppModule + Hooks staking.StakingHooksWrapper +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + feeCollectorName := in.Config.FeeCollectorName + if feeCollectorName == "" { + feeCollectorName = authtypes.FeeCollectorName + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + k := keeper.NewKeeper( + in.Cdc, + in.StoreService, + in.AccountKeeper, + in.BankKeeper, + in.StakingKeeper, + in.PoolKeeper, + feeCollectorName, + authority.String(), + ) + + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.PoolKeeper) + + return ModuleOutputs{ + DistrKeeper: k, + Module: m, + Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, + } +} diff --git a/x/distribution/module.go b/x/distribution/module.go index 690ed300d2e7..d5fa23d054c1 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -8,17 +8,11 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/distribution/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/distribution/client/cli" "cosmossdk.io/x/distribution/keeper" "cosmossdk.io/x/distribution/simulation" "cosmossdk.io/x/distribution/types" - staking "cosmossdk.io/x/staking/types" sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -37,7 +31,6 @@ var ( _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} @@ -117,9 +110,6 @@ func NewAppModule( } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -195,66 +185,3 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper, ) } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper - PoolKeeper types.PoolKeeper -} - -type ModuleOutputs struct { - depinject.Out - - DistrKeeper keeper.Keeper - Module appmodule.AppModule - Hooks staking.StakingHooksWrapper -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - feeCollectorName := in.Config.FeeCollectorName - if feeCollectorName == "" { - feeCollectorName = authtypes.FeeCollectorName - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - k := keeper.NewKeeper( - in.Cdc, - in.StoreService, - in.AccountKeeper, - in.BankKeeper, - in.StakingKeeper, - in.PoolKeeper, - feeCollectorName, - authority.String(), - ) - - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.PoolKeeper) - - return ModuleOutputs{ - DistrKeeper: k, - Module: m, - Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, - } -} diff --git a/x/evidence/depinject.go b/x/evidence/depinject.go new file mode 100644 index 000000000000..2fba5fa7a13f --- /dev/null +++ b/x/evidence/depinject.go @@ -0,0 +1,50 @@ +package evidence + +import ( + modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1" + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/evidence/keeper" + "cosmossdk.io/x/evidence/types" + + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + StoreService store.KVStoreService + Cdc codec.Codec + + StakingKeeper types.StakingKeeper + SlashingKeeper types.SlashingKeeper + AddressCodec address.Codec +} + +type ModuleOutputs struct { + depinject.Out + + EvidenceKeeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + k := keeper.NewKeeper(in.Cdc, in.StoreService, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec) + m := NewAppModule(*k) + + return ModuleOutputs{EvidenceKeeper: *k, Module: m} +} diff --git a/x/evidence/module.go b/x/evidence/module.go index 3f5ff820a6ad..fdd897b16483 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -9,12 +9,7 @@ import ( "github.com/spf13/cobra" "google.golang.org/grpc" - modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1" - "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - store "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" eviclient "cosmossdk.io/x/evidence/client" "cosmossdk.io/x/evidence/client/cli" "cosmossdk.io/x/evidence/keeper" @@ -120,9 +115,6 @@ func NewAppModule(keeper keeper.Keeper) AppModule { } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -174,38 +166,3 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return nil } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - StoreService store.KVStoreService - Cdc codec.Codec - - StakingKeeper types.StakingKeeper - SlashingKeeper types.SlashingKeeper - AddressCodec address.Codec -} - -type ModuleOutputs struct { - depinject.Out - - EvidenceKeeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - k := keeper.NewKeeper(in.Cdc, in.StoreService, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec) - m := NewAppModule(*k) - - return ModuleOutputs{EvidenceKeeper: *k, Module: m} -} diff --git a/x/feegrant/module/depinject.go b/x/feegrant/module/depinject.go new file mode 100644 index 000000000000..af86f6d7e491 --- /dev/null +++ b/x/feegrant/module/depinject.go @@ -0,0 +1,64 @@ +package module + +import ( + modulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/feegrant" + "cosmossdk.io/x/feegrant/keeper" + "cosmossdk.io/x/feegrant/simulation" + + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type FeegrantInputs struct { + depinject.In + + StoreService store.KVStoreService + Cdc codec.Codec + AccountKeeper feegrant.AccountKeeper + BankKeeper feegrant.BankKeeper + Registry cdctypes.InterfaceRegistry +} + +func ProvideModule(in FeegrantInputs) (keeper.Keeper, appmodule.AppModule) { + k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper) + m := NewAppModule(in.Cdc, in.AccountKeeper, in.BankKeeper, k, in.Registry) + return k, m +} + +// AppModuleSimulation functions + +// GenerateGenesisState creates a randomized GenState of the feegrant module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// RegisterStoreDecoder registers a decoder for feegrant module's types +func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { + sdr[feegrant.StoreKey] = simulation.NewDecodeStore(am.cdc) +} + +// WeightedOperations returns all the feegrant module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return simulation.WeightedOperations( + am.registry, simState.AppParams, simState.Cdc, simState.TxConfig, + am.accountKeeper, am.bankKeeper, am.keeper, am.accountKeeper.AddressCodec(), + ) +} diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index a76a0f835bd3..82f1f52a2e8f 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -8,22 +8,16 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "cosmossdk.io/errors" "cosmossdk.io/x/feegrant" "cosmossdk.io/x/feegrant/client/cli" "cosmossdk.io/x/feegrant/keeper" - "cosmossdk.io/x/feegrant/simulation" sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/types/module" - simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) var ( @@ -31,7 +25,6 @@ var ( _ module.AppModuleSimulation = AppModule{} _ module.HasServices = AppModule{} _ module.HasGenesis = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} @@ -126,9 +119,6 @@ func NewAppModule(cdc codec.Codec, ak feegrant.AccountKeeper, bk feegrant.BankKe } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -163,45 +153,3 @@ func (AppModule) ConsensusVersion() uint64 { return 2 } func (am AppModule) EndBlock(ctx context.Context) error { return EndBlocker(ctx, am.keeper) } - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type FeegrantInputs struct { - depinject.In - - StoreService store.KVStoreService - Cdc codec.Codec - AccountKeeper feegrant.AccountKeeper - BankKeeper feegrant.BankKeeper - Registry cdctypes.InterfaceRegistry -} - -func ProvideModule(in FeegrantInputs) (keeper.Keeper, appmodule.AppModule) { - k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper) - m := NewAppModule(in.Cdc, in.AccountKeeper, in.BankKeeper, k, in.Registry) - return k, m -} - -// AppModuleSimulation functions - -// GenerateGenesisState creates a randomized GenState of the feegrant module. -func (AppModule) GenerateGenesisState(simState *module.SimulationState) { - simulation.RandomizedGenState(simState) -} - -// RegisterStoreDecoder registers a decoder for feegrant module's types -func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { - sdr[feegrant.StoreKey] = simulation.NewDecodeStore(am.cdc) -} - -// WeightedOperations returns all the feegrant module operations with their respective weights. -func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - am.registry, simState.AppParams, simState.Cdc, simState.TxConfig, - am.accountKeeper, am.bankKeeper, am.keeper, am.accountKeeper.AddressCodec(), - ) -} diff --git a/x/genutil/depinject.go b/x/genutil/depinject.go new file mode 100644 index 000000000000..77320ca6b0c4 --- /dev/null +++ b/x/genutil/depinject.go @@ -0,0 +1,38 @@ +package genutil + +import ( + modulev1 "cosmossdk.io/api/cosmos/genutil/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/genesis" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +// ModuleInputs defines the inputs needed for the genutil module. +type ModuleInputs struct { + depinject.In + + AccountKeeper types.AccountKeeper + StakingKeeper types.StakingKeeper + DeliverTx genesis.TxHandler + Config client.TxConfig +} + +func ProvideModule(in ModuleInputs) appmodule.AppModule { + m := NewAppModule(in.AccountKeeper, in.StakingKeeper, in.DeliverTx, in.Config) + return m +} diff --git a/x/genutil/module.go b/x/genutil/module.go index 403838dddae9..6029f5b45acc 100644 --- a/x/genutil/module.go +++ b/x/genutil/module.go @@ -8,11 +8,8 @@ import ( abci "github.com/cometbft/cometbft/abci/types" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/genutil/module/v1" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/genesis" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -94,9 +91,6 @@ func NewAppModule(accountKeeper types.AccountKeeper, }) } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (AppModule) IsAppModule() {} @@ -119,24 +113,3 @@ func (am AppModule) ExportGenesis(_ context.Context, cdc codec.JSONCodec) json.R // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return 1 } - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -// ModuleInputs defines the inputs needed for the genutil module. -type ModuleInputs struct { - depinject.In - - AccountKeeper types.AccountKeeper - StakingKeeper types.StakingKeeper - DeliverTx genesis.TxHandler - Config client.TxConfig -} - -func ProvideModule(in ModuleInputs) appmodule.AppModule { - m := NewAppModule(in.AccountKeeper, in.StakingKeeper, in.DeliverTx, in.Config) - return m -} diff --git a/x/gov/depinject.go b/x/gov/depinject.go new file mode 100644 index 000000000000..40f2c9f30be5 --- /dev/null +++ b/x/gov/depinject.go @@ -0,0 +1,135 @@ +package gov + +import ( + "fmt" + "sort" + "strings" + + "golang.org/x/exp/maps" + "golang.org/x/exp/slices" + + modulev1 "cosmossdk.io/api/cosmos/gov/module/v1" + "cosmossdk.io/core/appmodule" + store "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/gov/keeper" + govtypes "cosmossdk.io/x/gov/types" + "cosmossdk.io/x/gov/types/v1beta1" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Invoke(InvokeAddRoutes, InvokeSetHooks), + appconfig.Provide(ProvideModule)) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + Cdc codec.Codec + StoreService store.KVStoreService + ModuleKey depinject.OwnModuleKey + MsgServiceRouter baseapp.MessageRouter + + AccountKeeper govtypes.AccountKeeper + BankKeeper govtypes.BankKeeper + StakingKeeper govtypes.StakingKeeper + PoolKeeper govtypes.PoolKeeper +} + +type ModuleOutputs struct { + depinject.Out + + Module appmodule.AppModule + Keeper *keeper.Keeper + HandlerRoute v1beta1.HandlerRoute +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + defaultConfig := govtypes.DefaultConfig() + if in.Config.MaxTitleLen != 0 { + defaultConfig.MaxTitleLen = in.Config.MaxTitleLen + } + if in.Config.MaxMetadataLen != 0 { + defaultConfig.MaxMetadataLen = in.Config.MaxMetadataLen + } + if in.Config.MaxSummaryLen != 0 { + defaultConfig.MaxSummaryLen = in.Config.MaxSummaryLen + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(govtypes.ModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + k := keeper.NewKeeper( + in.Cdc, + in.StoreService, + in.AccountKeeper, + in.BankKeeper, + in.StakingKeeper, + in.PoolKeeper, + in.MsgServiceRouter, + defaultConfig, + authority.String(), + ) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.PoolKeeper) + hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: govtypes.RouterKey} + + return ModuleOutputs{Module: m, Keeper: k, HandlerRoute: hr} +} + +func InvokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { + if keeper == nil || routes == nil { + return + } + + // Default route order is a lexical sort by RouteKey. + // Explicit ordering can be added to the module config if required. + slices.SortFunc(routes, func(x, y v1beta1.HandlerRoute) int { + return strings.Compare(x.RouteKey, y.RouteKey) + }) + + router := v1beta1.NewRouter() + for _, r := range routes { + router.AddRoute(r.RouteKey, r.Handler) + } + keeper.SetLegacyRouter(router) +} + +func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]govtypes.GovHooksWrapper) error { + if keeper == nil || govHooks == nil { + return nil + } + + // Default ordering is lexical by module name. + // Explicit ordering can be added to the module config if required. + modNames := maps.Keys(govHooks) + order := modNames + sort.Strings(order) + + var multiHooks govtypes.MultiGovHooks + for _, modName := range order { + hook, ok := govHooks[modName] + if !ok { + return fmt.Errorf("can't find staking hooks for module %s", modName) + } + multiHooks = append(multiHooks, hook) + } + + keeper.SetHooks(multiHooks) + return nil +} diff --git a/x/gov/module.go b/x/gov/module.go index f6b1750a7b55..52fb641283bd 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -4,21 +4,12 @@ import ( "context" "encoding/json" "fmt" - "sort" - "strings" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" - modulev1 "cosmossdk.io/api/cosmos/gov/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - store "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" govclient "cosmossdk.io/x/gov/client" "cosmossdk.io/x/gov/client/cli" "cosmossdk.io/x/gov/keeper" @@ -27,7 +18,6 @@ import ( v1 "cosmossdk.io/x/gov/types/v1" "cosmossdk.io/x/gov/types/v1beta1" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -145,119 +135,9 @@ func NewAppModule( } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} -func init() { - am.Register( - &modulev1.Module{}, - am.Invoke(InvokeAddRoutes, InvokeSetHooks), - am.Provide(ProvideModule)) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - Cdc codec.Codec - StoreService store.KVStoreService - ModuleKey depinject.OwnModuleKey - MsgServiceRouter baseapp.MessageRouter - - AccountKeeper govtypes.AccountKeeper - BankKeeper govtypes.BankKeeper - StakingKeeper govtypes.StakingKeeper - PoolKeeper govtypes.PoolKeeper -} - -type ModuleOutputs struct { - depinject.Out - - Module appmodule.AppModule - Keeper *keeper.Keeper - HandlerRoute v1beta1.HandlerRoute -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - defaultConfig := govtypes.DefaultConfig() - if in.Config.MaxTitleLen != 0 { - defaultConfig.MaxTitleLen = in.Config.MaxTitleLen - } - if in.Config.MaxMetadataLen != 0 { - defaultConfig.MaxMetadataLen = in.Config.MaxMetadataLen - } - if in.Config.MaxSummaryLen != 0 { - defaultConfig.MaxSummaryLen = in.Config.MaxSummaryLen - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(govtypes.ModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - k := keeper.NewKeeper( - in.Cdc, - in.StoreService, - in.AccountKeeper, - in.BankKeeper, - in.StakingKeeper, - in.PoolKeeper, - in.MsgServiceRouter, - defaultConfig, - authority.String(), - ) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.PoolKeeper) - hr := v1beta1.HandlerRoute{Handler: v1beta1.ProposalHandler, RouteKey: govtypes.RouterKey} - - return ModuleOutputs{Module: m, Keeper: k, HandlerRoute: hr} -} - -func InvokeAddRoutes(keeper *keeper.Keeper, routes []v1beta1.HandlerRoute) { - if keeper == nil || routes == nil { - return - } - - // Default route order is a lexical sort by RouteKey. - // Explicit ordering can be added to the module config if required. - slices.SortFunc(routes, func(x, y v1beta1.HandlerRoute) int { - return strings.Compare(x.RouteKey, y.RouteKey) - }) - - router := v1beta1.NewRouter() - for _, r := range routes { - router.AddRoute(r.RouteKey, r.Handler) - } - keeper.SetLegacyRouter(router) -} - -func InvokeSetHooks(keeper *keeper.Keeper, govHooks map[string]govtypes.GovHooksWrapper) error { - if keeper == nil || govHooks == nil { - return nil - } - - // Default ordering is lexical by module name. - // Explicit ordering can be added to the module config if required. - modNames := maps.Keys(govHooks) - order := modNames - sort.Strings(order) - - var multiHooks govtypes.MultiGovHooks - for _, modName := range order { - hook, ok := govHooks[modName] - if !ok { - return fmt.Errorf("can't find staking hooks for module %s", modName) - } - multiHooks = append(multiHooks, hook) - } - - keeper.SetHooks(multiHooks) - return nil -} - // RegisterInvariants registers module invariants func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { keeper.RegisterInvariants(ir, am.keeper, am.bankKeeper) diff --git a/x/group/module/depinject.go b/x/group/module/depinject.go new file mode 100644 index 000000000000..f006df24f345 --- /dev/null +++ b/x/group/module/depinject.go @@ -0,0 +1,62 @@ +package module + +import ( + modulev1 "cosmossdk.io/api/cosmos/group/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + store "cosmossdk.io/store/types" + "cosmossdk.io/x/group" + "cosmossdk.io/x/group/keeper" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type GroupInputs struct { + depinject.In + + Config *modulev1.Module + Key *store.KVStoreKey + Cdc codec.Codec + AccountKeeper group.AccountKeeper + BankKeeper group.BankKeeper + Registry cdctypes.InterfaceRegistry + MsgServiceRouter baseapp.MessageRouter +} + +type GroupOutputs struct { + depinject.Out + + GroupKeeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in GroupInputs) GroupOutputs { + k := keeper.NewKeeper(in.Key, + in.Cdc, + in.MsgServiceRouter, + in.AccountKeeper, + group.Config{ + MaxExecutionPeriod: in.Config.MaxExecutionPeriod.AsDuration(), + MaxMetadataLen: in.Config.MaxMetadataLen, + MaxProposalTitleLen: in.Config.MaxProposalTitleLen, + MaxProposalSummaryLen: in.Config.MaxProposalSummaryLen, + }, + ) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) + return GroupOutputs{GroupKeeper: k, Module: m} +} diff --git a/x/group/module/module.go b/x/group/module/module.go index 7ce3c2ed217f..a91c618f58bc 100644 --- a/x/group/module/module.go +++ b/x/group/module/module.go @@ -8,18 +8,13 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/group/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - store "cosmossdk.io/store/types" "cosmossdk.io/x/group" "cosmossdk.io/x/group/client/cli" "cosmossdk.io/x/group/keeper" "cosmossdk.io/x/group/simulation" - "github.com/cosmos/cosmos-sdk/baseapp" sdkclient "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -37,7 +32,6 @@ var ( _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} _ module.HasInvariants = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasEndBlocker = AppModule{} @@ -62,9 +56,6 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak group.AccountKeeper, } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -176,49 +167,3 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp am.accKeeper, am.bankKeeper, am.keeper, am.cdc, ) } - -// -// App Wiring Setup -// - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type GroupInputs struct { - depinject.In - - Config *modulev1.Module - Key *store.KVStoreKey - Cdc codec.Codec - AccountKeeper group.AccountKeeper - BankKeeper group.BankKeeper - Registry cdctypes.InterfaceRegistry - MsgServiceRouter baseapp.MessageRouter -} - -type GroupOutputs struct { - depinject.Out - - GroupKeeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in GroupInputs) GroupOutputs { - k := keeper.NewKeeper(in.Key, - in.Cdc, - in.MsgServiceRouter, - in.AccountKeeper, - group.Config{ - MaxExecutionPeriod: in.Config.MaxExecutionPeriod.AsDuration(), - MaxMetadataLen: in.Config.MaxMetadataLen, - MaxProposalTitleLen: in.Config.MaxProposalTitleLen, - MaxProposalSummaryLen: in.Config.MaxProposalSummaryLen, - }, - ) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) - return GroupOutputs{GroupKeeper: k, Module: m} -} diff --git a/x/mint/depinject.go b/x/mint/depinject.go new file mode 100644 index 000000000000..d2c5112a4242 --- /dev/null +++ b/x/mint/depinject.go @@ -0,0 +1,79 @@ +package mint + +import ( + modulev1 "cosmossdk.io/api/cosmos/mint/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/mint/keeper" + "cosmossdk.io/x/mint/types" + + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + ModuleKey depinject.OwnModuleKey + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + InflationCalculationFn types.InflationCalculationFn `optional:"true"` + + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + StakingKeeper types.StakingKeeper +} + +type ModuleOutputs struct { + depinject.Out + + MintKeeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + feeCollectorName := in.Config.FeeCollectorName + if feeCollectorName == "" { + feeCollectorName = authtypes.FeeCollectorName + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + + k := keeper.NewKeeper( + in.Cdc, + in.StoreService, + in.StakingKeeper, + in.AccountKeeper, + in.BankKeeper, + feeCollectorName, + as, + ) + + // when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.InflationCalculationFn) + + return ModuleOutputs{MintKeeper: k, Module: m} +} diff --git a/x/mint/module.go b/x/mint/module.go index 4af6eeaa349f..0f1e3492cac1 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -7,12 +7,7 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/mint/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/mint/keeper" "cosmossdk.io/x/mint/simulation" "cosmossdk.io/x/mint/types" @@ -32,7 +27,6 @@ var ( _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} _ appmodule.HasBeginBlocker = AppModule{} @@ -113,9 +107,6 @@ func NewAppModule( } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -177,67 +168,3 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { return nil } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - ModuleKey depinject.OwnModuleKey - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - InflationCalculationFn types.InflationCalculationFn `optional:"true"` - - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper -} - -type ModuleOutputs struct { - depinject.Out - - MintKeeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - feeCollectorName := in.Config.FeeCollectorName - if feeCollectorName == "" { - feeCollectorName = authtypes.FeeCollectorName - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) - if err != nil { - panic(err) - } - - k := keeper.NewKeeper( - in.Cdc, - in.StoreService, - in.StakingKeeper, - in.AccountKeeper, - in.BankKeeper, - feeCollectorName, - as, - ) - - // when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.InflationCalculationFn) - - return ModuleOutputs{MintKeeper: k, Module: m} -} diff --git a/x/nft/module/depinject.go b/x/nft/module/depinject.go new file mode 100644 index 000000000000..468a64beb726 --- /dev/null +++ b/x/nft/module/depinject.go @@ -0,0 +1,50 @@ +package module + +import ( + modulev1 "cosmossdk.io/api/cosmos/nft/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + "cosmossdk.io/x/nft" + "cosmossdk.io/x/nft/keeper" + + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + StoreService store.KVStoreService + Cdc codec.Codec + Registry cdctypes.InterfaceRegistry + + AccountKeeper nft.AccountKeeper + BankKeeper nft.BankKeeper +} + +type ModuleOutputs struct { + depinject.Out + + NFTKeeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + k := keeper.NewKeeper(in.StoreService, in.Cdc, in.AccountKeeper, in.BankKeeper) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) + + return ModuleOutputs{NFTKeeper: k, Module: m} +} diff --git a/x/nft/module/module.go b/x/nft/module/module.go index 79cc78dff3aa..3173ede06b56 100644 --- a/x/nft/module/module.go +++ b/x/nft/module/module.go @@ -7,12 +7,8 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" - modulev1 "cosmossdk.io/api/cosmos/nft/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" "cosmossdk.io/errors" "cosmossdk.io/x/nft" "cosmossdk.io/x/nft/keeper" @@ -104,9 +100,6 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak nft.AccountKeeper, b } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -150,38 +143,3 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp am.accountKeeper, am.bankKeeper, am.keeper, ) } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type NftInputs struct { - depinject.In - - StoreService store.KVStoreService - Cdc codec.Codec - Registry cdctypes.InterfaceRegistry - - AccountKeeper nft.AccountKeeper - BankKeeper nft.BankKeeper -} - -type NftOutputs struct { - depinject.Out - - NFTKeeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in NftInputs) NftOutputs { - k := keeper.NewKeeper(in.StoreService, in.Cdc, in.AccountKeeper, in.BankKeeper) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry) - - return NftOutputs{NFTKeeper: k, Module: m} -} diff --git a/x/params/depinject.go b/x/params/depinject.go new file mode 100644 index 000000000000..ad94b5c1d410 --- /dev/null +++ b/x/params/depinject.go @@ -0,0 +1,71 @@ +package params + +import ( + modulev1 "cosmossdk.io/api/cosmos/params/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + store "cosmossdk.io/store/types" + govv1beta1 "cosmossdk.io/x/gov/types/v1beta1" + "cosmossdk.io/x/params/keeper" + "cosmossdk.io/x/params/types" + "cosmossdk.io/x/params/types/proposal" + + "github.com/cosmos/cosmos-sdk/codec" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide( + ProvideModule, + ProvideSubspace, + )) +} + +type ModuleInputs struct { + depinject.In + + KvStoreKey *store.KVStoreKey + TransientStoreKey *store.TransientStoreKey + Cdc codec.Codec + LegacyAmino *codec.LegacyAmino +} + +type ModuleOutputs struct { + depinject.Out + + ParamsKeeper keeper.Keeper + Module appmodule.AppModule + GovHandler govv1beta1.HandlerRoute +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.KvStoreKey, in.TransientStoreKey) + + m := NewAppModule(k) + govHandler := govv1beta1.HandlerRoute{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)} + + return ModuleOutputs{ParamsKeeper: k, Module: m, GovHandler: govHandler} +} + +type SubspaceInputs struct { + depinject.In + + Key depinject.ModuleKey + Keeper keeper.Keeper + KeyTables map[string]types.KeyTable +} + +func ProvideSubspace(in SubspaceInputs) types.Subspace { + moduleName := in.Key.Name() + kt, exists := in.KeyTables[moduleName] + if !exists { + return in.Keeper.Subspace(moduleName) + } + return in.Keeper.Subspace(moduleName).WithKeyTable(kt) +} diff --git a/x/params/module.go b/x/params/module.go index 13ee912d94f8..e2b2ac61f539 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -5,14 +5,8 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/params/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - store "cosmossdk.io/store/types" - govv1beta1 "cosmossdk.io/x/gov/types/v1beta1" "cosmossdk.io/x/params/keeper" - "cosmossdk.io/x/params/types" "cosmossdk.io/x/params/types/proposal" "github.com/cosmos/cosmos-sdk/client" @@ -26,7 +20,6 @@ var ( _ module.AppModuleBasic = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasServices = AppModule{} - _ depinject.OnePerModuleType = AppModule{} _ appmodule.AppModule = AppModule{} ) @@ -73,9 +66,6 @@ func NewAppModule(k keeper.Keeper) AppModule { } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -98,58 +88,3 @@ func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weig // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide( - ProvideModule, - ProvideSubspace, - )) -} - -type ModuleInputs struct { - depinject.In - - KvStoreKey *store.KVStoreKey - TransientStoreKey *store.TransientStoreKey - Cdc codec.Codec - LegacyAmino *codec.LegacyAmino -} - -type ModuleOutputs struct { - depinject.Out - - ParamsKeeper keeper.Keeper - Module appmodule.AppModule - GovHandler govv1beta1.HandlerRoute -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.KvStoreKey, in.TransientStoreKey) - - m := NewAppModule(k) - govHandler := govv1beta1.HandlerRoute{RouteKey: proposal.RouterKey, Handler: NewParamChangeProposalHandler(k)} - - return ModuleOutputs{ParamsKeeper: k, Module: m, GovHandler: govHandler} -} - -type SubspaceInputs struct { - depinject.In - - Key depinject.ModuleKey - Keeper keeper.Keeper - KeyTables map[string]types.KeyTable -} - -func ProvideSubspace(in SubspaceInputs) types.Subspace { - moduleName := in.Key.Name() - kt, exists := in.KeyTables[moduleName] - if !exists { - return in.Keeper.Subspace(moduleName) - } - return in.Keeper.Subspace(moduleName).WithKeyTable(kt) -} diff --git a/x/protocolpool/depinject.go b/x/protocolpool/depinject.go new file mode 100644 index 000000000000..a49ebc0f5952 --- /dev/null +++ b/x/protocolpool/depinject.go @@ -0,0 +1,89 @@ +package protocolpool + +import ( + modulev1 "cosmossdk.io/api/cosmos/protocolpool/module/v1" + "cosmossdk.io/core/appmodule" + storetypes "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/protocolpool/keeper" + "cosmossdk.io/x/protocolpool/simulation" + "cosmossdk.io/x/protocolpool/types" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + Codec codec.Codec + StoreService storetypes.KVStoreService + + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + StakingKeeper types.StakingKeeper +} + +type ModuleOutputs struct { + depinject.Out + + Keeper keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress("gov") + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + k := keeper.NewKeeper(in.Codec, in.StoreService, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, authority.String()) + m := NewAppModule(in.Codec, k, in.AccountKeeper, in.BankKeeper) + + return ModuleOutputs{ + Keeper: k, + Module: m, + } +} + +// ____________________________________________________________________________ + +// AppModuleSimulation functions + +// GenerateGenesisState creates a randomized GenState of the protocolpool module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { +} + +// RegisterStoreDecoder registers a decoder for protocolpool module's types +func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { +} + +// ProposalMsgs returns all the protocolpool msgs used to simulate governance proposals. +func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { + return simulation.ProposalMsgs() +} + +// WeightedOperations returns the all the protocolpool module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return simulation.WeightedOperations( + simState.AppParams, simState.Cdc, simState.TxConfig, + am.accountKeeper, am.bankKeeper, am.keeper, + ) +} diff --git a/x/protocolpool/module.go b/x/protocolpool/module.go index 94dc10334f7b..ae4efdc4e9a4 100644 --- a/x/protocolpool/module.go +++ b/x/protocolpool/module.go @@ -7,21 +7,14 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/protocolpool/module/v1" "cosmossdk.io/core/appmodule" - storetypes "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/protocolpool/keeper" - "cosmossdk.io/x/protocolpool/simulation" "cosmossdk.io/x/protocolpool/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/types/module" - simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) // ConsensusVersion defines the current x/protocolpool module consensus version. @@ -85,9 +78,6 @@ type AppModule struct { var _ appmodule.AppModule = AppModule{} -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -130,74 +120,3 @@ func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } - -// -// App Wiring Setup -// - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - Codec codec.Codec - StoreService storetypes.KVStoreService - - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper -} - -type ModuleOutputs struct { - depinject.Out - - Keeper keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress("gov") - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - k := keeper.NewKeeper(in.Codec, in.StoreService, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, authority.String()) - m := NewAppModule(in.Codec, k, in.AccountKeeper, in.BankKeeper) - - return ModuleOutputs{ - Keeper: k, - Module: m, - } -} - -// ____________________________________________________________________________ - -// AppModuleSimulation functions - -// GenerateGenesisState creates a randomized GenState of the protocolpool module. -func (AppModule) GenerateGenesisState(simState *module.SimulationState) { -} - -// RegisterStoreDecoder registers a decoder for protocolpool module's types -func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { -} - -// ProposalMsgs returns all the protocolpool msgs used to simulate governance proposals. -func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { - return simulation.ProposalMsgs() -} - -// WeightedOperations returns the all the protocolpool module operations with their respective weights. -func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, simState.TxConfig, - am.accountKeeper, am.bankKeeper, am.keeper, - ) -} diff --git a/x/slashing/depinject.go b/x/slashing/depinject.go new file mode 100644 index 000000000000..3da03a787b26 --- /dev/null +++ b/x/slashing/depinject.go @@ -0,0 +1,73 @@ +package slashing + +import ( + "fmt" + + modulev1 "cosmossdk.io/api/cosmos/slashing/module/v1" + "cosmossdk.io/core/appmodule" + store "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/slashing/keeper" + "cosmossdk.io/x/slashing/types" + staking "cosmossdk.io/x/staking/types" + + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + LegacyAmino *codec.LegacyAmino + Registry cdctypes.InterfaceRegistry + + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + StakingKeeper types.StakingKeeper +} + +type ModuleOutputs struct { + depinject.Out + + Keeper keeper.Keeper + Module appmodule.AppModule + Hooks staking.StakingHooksWrapper +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(fmt.Errorf("unable to decode authority in slashing: %w", err)) + } + + k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.StoreService, in.StakingKeeper, authStr) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.Registry) + return ModuleOutputs{ + Keeper: k, + Module: m, + Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, + } +} diff --git a/x/slashing/module.go b/x/slashing/module.go index 6a984253745d..ea2b4c9ad947 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -7,16 +7,10 @@ import ( gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - modulev1 "cosmossdk.io/api/cosmos/slashing/module/v1" "cosmossdk.io/core/appmodule" - store "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/slashing/keeper" "cosmossdk.io/x/slashing/simulation" "cosmossdk.io/x/slashing/types" - staking "cosmossdk.io/x/staking/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -111,9 +105,6 @@ func NewAppModule( } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -182,57 +173,3 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper, ) } - -// -// App Wiring Setup -// - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - LegacyAmino *codec.LegacyAmino - Registry cdctypes.InterfaceRegistry - - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - StakingKeeper types.StakingKeeper -} - -type ModuleOutputs struct { - depinject.Out - - Keeper keeper.Keeper - Module appmodule.AppModule - Hooks staking.StakingHooksWrapper -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) - if err != nil { - panic(fmt.Errorf("unable to decode authority in slashing: %w", err)) - } - - k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.StoreService, in.StakingKeeper, authStr) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.Registry) - return ModuleOutputs{ - Keeper: k, - Module: m, - Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, - } -} diff --git a/x/staking/depinject.go b/x/staking/depinject.go new file mode 100644 index 000000000000..cce30c34783f --- /dev/null +++ b/x/staking/depinject.go @@ -0,0 +1,145 @@ +package staking + +import ( + "fmt" + "sort" + + "golang.org/x/exp/maps" + + modulev1 "cosmossdk.io/api/cosmos/staking/module/v1" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/staking/keeper" + "cosmossdk.io/x/staking/simulation" + "cosmossdk.io/x/staking/types" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule( + &modulev1.Module{}, + appconfig.Provide(ProvideModule), + appconfig.Invoke(InvokeSetStakingHooks), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + ValidatorAddressCodec runtime.ValidatorAddressCodec + ConsensusAddressCodec runtime.ConsensusAddressCodec + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + Cdc codec.Codec + StoreService store.KVStoreService +} + +// Dependency Injection Outputs +type ModuleOutputs struct { + depinject.Out + + StakingKeeper *keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + + k := keeper.NewKeeper( + in.Cdc, + in.StoreService, + in.AccountKeeper, + in.BankKeeper, + as, + in.ValidatorAddressCodec, + in.ConsensusAddressCodec, + ) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper) + return ModuleOutputs{StakingKeeper: k, Module: m} +} + +func InvokeSetStakingHooks( + config *modulev1.Module, + keeper *keeper.Keeper, + stakingHooks map[string]types.StakingHooksWrapper, +) error { + // all arguments to invokers are optional + if keeper == nil || config == nil { + return nil + } + + modNames := maps.Keys(stakingHooks) + order := config.HooksOrder + if len(order) == 0 { + order = modNames + sort.Strings(order) + } + + if len(order) != len(modNames) { + return fmt.Errorf("len(hooks_order: %v) != len(hooks modules: %v)", order, modNames) + } + + if len(modNames) == 0 { + return nil + } + + var multiHooks types.MultiStakingHooks + for _, modName := range order { + hook, ok := stakingHooks[modName] + if !ok { + return fmt.Errorf("can't find staking hooks for module %s", modName) + } + + multiHooks = append(multiHooks, hook) + } + + keeper.SetHooks(multiHooks) + return nil +} + +// AppModuleSimulation functions + +// GenerateGenesisState creates a randomized GenState of the staking module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// ProposalMsgs returns msgs used for governance proposals for simulations. +func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { + return simulation.ProposalMsgs() +} + +// RegisterStoreDecoder registers a decoder for staking module's types +func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { + sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) +} + +// WeightedOperations returns the all the staking module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return simulation.WeightedOperations( + simState.AppParams, simState.Cdc, simState.TxConfig, + am.accountKeeper, am.bankKeeper, am.keeper, + ) +} diff --git a/x/staking/module.go b/x/staking/module.go index 333d89d53b05..bc2841106ef1 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -4,31 +4,22 @@ import ( "context" "encoding/json" "fmt" - "sort" abci "github.com/cometbft/cometbft/abci/types" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" - "golang.org/x/exp/maps" - modulev1 "cosmossdk.io/api/cosmos/staking/module/v1" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/staking/client/cli" "cosmossdk.io/x/staking/keeper" - "cosmossdk.io/x/staking/simulation" "cosmossdk.io/x/staking/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" - simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) const ( @@ -120,9 +111,6 @@ func NewAppModule( } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -180,120 +168,3 @@ func (am AppModule) BeginBlock(ctx context.Context) error { func (am AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) { return am.keeper.EndBlocker(ctx) } - -func init() { - am.Register( - &modulev1.Module{}, - am.Provide(ProvideModule), - am.Invoke(InvokeSetStakingHooks), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - ValidatorAddressCodec runtime.ValidatorAddressCodec - ConsensusAddressCodec runtime.ConsensusAddressCodec - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - Cdc codec.Codec - StoreService store.KVStoreService -} - -// Dependency Injection Outputs -type ModuleOutputs struct { - depinject.Out - - StakingKeeper *keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) - if err != nil { - panic(err) - } - - k := keeper.NewKeeper( - in.Cdc, - in.StoreService, - in.AccountKeeper, - in.BankKeeper, - as, - in.ValidatorAddressCodec, - in.ConsensusAddressCodec, - ) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper) - return ModuleOutputs{StakingKeeper: k, Module: m} -} - -func InvokeSetStakingHooks( - config *modulev1.Module, - keeper *keeper.Keeper, - stakingHooks map[string]types.StakingHooksWrapper, -) error { - // all arguments to invokers are optional - if keeper == nil || config == nil { - return nil - } - - modNames := maps.Keys(stakingHooks) - order := config.HooksOrder - if len(order) == 0 { - order = modNames - sort.Strings(order) - } - - if len(order) != len(modNames) { - return fmt.Errorf("len(hooks_order: %v) != len(hooks modules: %v)", order, modNames) - } - - if len(modNames) == 0 { - return nil - } - - var multiHooks types.MultiStakingHooks - for _, modName := range order { - hook, ok := stakingHooks[modName] - if !ok { - return fmt.Errorf("can't find staking hooks for module %s", modName) - } - - multiHooks = append(multiHooks, hook) - } - - keeper.SetHooks(multiHooks) - return nil -} - -// AppModuleSimulation functions - -// GenerateGenesisState creates a randomized GenState of the staking module. -func (AppModule) GenerateGenesisState(simState *module.SimulationState) { - simulation.RandomizedGenState(simState) -} - -// ProposalMsgs returns msgs used for governance proposals for simulations. -func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { - return simulation.ProposalMsgs() -} - -// RegisterStoreDecoder registers a decoder for staking module's types -func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { - sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) -} - -// WeightedOperations returns the all the staking module operations with their respective weights. -func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, simState.TxConfig, - am.accountKeeper, am.bankKeeper, am.keeper, - ) -} diff --git a/x/upgrade/depinject.go b/x/upgrade/depinject.go new file mode 100644 index 000000000000..07ed051ceaaf --- /dev/null +++ b/x/upgrade/depinject.go @@ -0,0 +1,93 @@ +package upgrade + +import ( + "github.com/spf13/cast" + + modulev1 "cosmossdk.io/api/cosmos/upgrade/module/v1" + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" + "cosmossdk.io/depinject" + "cosmossdk.io/depinject/appconfig" + authtypes "cosmossdk.io/x/auth/types" + "cosmossdk.io/x/upgrade/keeper" + "cosmossdk.io/x/upgrade/types" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/server" + servertypes "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/types/module" +) + +var _ depinject.OnePerModuleType = AppModule{} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() {} + +func init() { + appconfig.RegisterModule(&modulev1.Module{}, + appconfig.Provide(ProvideModule), + appconfig.Invoke(PopulateVersionMap), + ) +} + +type ModuleInputs struct { + depinject.In + + Config *modulev1.Module + StoreService store.KVStoreService + Cdc codec.Codec + AddressCodec address.Codec + AppVersionModifier baseapp.AppVersionModifier + + AppOpts servertypes.AppOptions `optional:"true"` +} + +type ModuleOutputs struct { + depinject.Out + + UpgradeKeeper *keeper.Keeper + Module appmodule.AppModule +} + +func ProvideModule(in ModuleInputs) ModuleOutputs { + var ( + homePath string + skipUpgradeHeights = make(map[int64]bool) + ) + + if in.AppOpts != nil { + for _, h := range cast.ToIntSlice(in.AppOpts.Get(server.FlagUnsafeSkipUpgrades)) { + skipUpgradeHeights[int64(h)] = true + } + + homePath = cast.ToString(in.AppOpts.Get(flags.FlagHome)) + } + + // default to governance authority if not provided + authority := authtypes.NewModuleAddress(types.GovModuleName) + if in.Config.Authority != "" { + authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) + } + + auth, err := in.AddressCodec.BytesToString(authority) + if err != nil { + panic(err) + } + + // set the governance module account as the authority for conducting upgrades + k := keeper.NewKeeper(skipUpgradeHeights, in.StoreService, in.Cdc, homePath, in.AppVersionModifier, auth) + m := NewAppModule(k, in.AddressCodec) + + return ModuleOutputs{UpgradeKeeper: k, Module: m} +} + +func PopulateVersionMap(upgradeKeeper *keeper.Keeper, modules map[string]appmodule.AppModule) { + if upgradeKeeper == nil { + return + } + + upgradeKeeper.SetInitVersionMap(module.NewManagerFromMap(modules).GetVersionMap()) +} diff --git a/x/upgrade/module.go b/x/upgrade/module.go index e821a4d8faa5..ea647b5e1d94 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -6,27 +6,17 @@ import ( "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/spf13/cast" "github.com/spf13/cobra" - modulev1 "cosmossdk.io/api/cosmos/upgrade/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" - "cosmossdk.io/depinject" - am "cosmossdk.io/depinject/appmodule" - authtypes "cosmossdk.io/x/auth/types" "cosmossdk.io/x/upgrade/client/cli" "cosmossdk.io/x/upgrade/keeper" "cosmossdk.io/x/upgrade/types" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/server" - servertypes "github.com/cosmos/cosmos-sdk/server/types" "github.com/cosmos/cosmos-sdk/types/module" ) @@ -90,9 +80,6 @@ func NewAppModule(keeper *keeper.Keeper, ac address.Codec) AppModule { } } -// IsOnePerModuleType implements the depinject.OnePerModuleType interface. -func (am AppModule) IsOnePerModuleType() {} - // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} @@ -160,73 +147,3 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } func (am AppModule) PreBlock(ctx context.Context) (appmodule.ResponsePreBlock, error) { return PreBlocker(ctx, am.keeper) } - -// -// App Wiring Setup -// - -func init() { - am.Register(&modulev1.Module{}, - am.Provide(ProvideModule), - am.Invoke(PopulateVersionMap), - ) -} - -type ModuleInputs struct { - depinject.In - - Config *modulev1.Module - StoreService store.KVStoreService - Cdc codec.Codec - AddressCodec address.Codec - AppVersionModifier baseapp.AppVersionModifier - - AppOpts servertypes.AppOptions `optional:"true"` -} - -type ModuleOutputs struct { - depinject.Out - - UpgradeKeeper *keeper.Keeper - Module appmodule.AppModule -} - -func ProvideModule(in ModuleInputs) ModuleOutputs { - var ( - homePath string - skipUpgradeHeights = make(map[int64]bool) - ) - - if in.AppOpts != nil { - for _, h := range cast.ToIntSlice(in.AppOpts.Get(server.FlagUnsafeSkipUpgrades)) { - skipUpgradeHeights[int64(h)] = true - } - - homePath = cast.ToString(in.AppOpts.Get(flags.FlagHome)) - } - - // default to governance authority if not provided - authority := authtypes.NewModuleAddress(types.GovModuleName) - if in.Config.Authority != "" { - authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) - } - - auth, err := in.AddressCodec.BytesToString(authority) - if err != nil { - panic(err) - } - - // set the governance module account as the authority for conducting upgrades - k := keeper.NewKeeper(skipUpgradeHeights, in.StoreService, in.Cdc, homePath, in.AppVersionModifier, auth) - m := NewAppModule(k, in.AddressCodec) - - return ModuleOutputs{UpgradeKeeper: k, Module: m} -} - -func PopulateVersionMap(upgradeKeeper *keeper.Keeper, modules map[string]appmodule.AppModule) { - if upgradeKeeper == nil { - return - } - - upgradeKeeper.SetInitVersionMap(module.NewManagerFromMap(modules).GetVersionMap()) -}