From 9fcc2f224c21e4a6ad28420ab4b61a569e031d0c Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Thu, 12 Sep 2024 12:06:14 +0300 Subject: [PATCH 1/8] feat(baseapp/docs): add instructions to change DefaultGenesis --- docs/build/building-apps/01-app-go-di.md | 102 +++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/docs/build/building-apps/01-app-go-di.md b/docs/build/building-apps/01-app-go-di.md index d77899d26e6d..3f9288be4af6 100644 --- a/docs/build/building-apps/01-app-go-di.md +++ b/docs/build/building-apps/01-app-go-di.md @@ -118,6 +118,108 @@ More information on how work `depinject.Configs` and `depinject.Supply` can be f https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/simapp/app_v2.go#L114-L146 ``` +### Modifying the `DefaultGenesis` + +It is possible to modify the DefaultGenesis parameters for modules by wrapping the module and + +Previously, the `ModuleBasics` was a global variable that was used to register all modules' `AppModuleBasic` implementation. + +Example ( staking ): + +```go +type StakingModule struct { + staking.AppModuleBasic +} + +// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. +func (StakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { + params := stakingtypes.DefaultParams() + params.BondDenom = "mydenom" + + return cdc.MustMarshalJSON(&stakingtypes.GenesisState{ + Params: params, + }) +} + +// option 1: using the previously removed global variable +ModuleBasics = module.NewBasicManager( + StakingModule{}, // wrapped staking module + auth.AppModuleBasic{}, + + distr.AppModuleBasic{}, + params.AppModuleBasic{}, + feegrantmodule.AppModuleBasic{}, + authzmodule.AppModuleBasic{}, + ibc.AppModuleBasic{}, + upgrade.AppModuleBasic{}, + evidence.AppModuleBasic{}, + ... + ) + +// option 2: the basic module manager can be now created from the module manager +app.BasicModuleManager = module.NewBasicManagerFromManager( + app.moduleManager, + // override staking AppModule + map[string]module.AppModuleBasic{ + stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}}, + }, +) + +// option 3: using depinject +depinject.Supply( + // supply custom module basics + map[string]module.AppModuleBasic{ + stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}}, + }, +) + +``` +The basic module manager has been deleted. It was not necessary anymore and was simplified to use the `module.Manager` directly. + +Example ( staking ) : + +```go +type CustomStakingModule struct { + staking.AppModule + cdc codec.Codec +} + +// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. +func (cm CustomStakingModule) DefaultGenesis() json.RawMessage { + params := stakingtypes.DefaultParams() + params.BondDenom = "mydenom" + + return cm.cdc.MustMarshalJSON(&stakingtypes.GenesisState{ + Params: params, + }) +} + +// option 1: use new module manager +moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ + stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, + // other modules ... +}) + +// option 2: override previous module manager +oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule) +moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ + AppModule: oldStakingModule, + cdc: appCodec, +} + + +// depinject users +depinject.Inject( + // ... + &moduleManager, + ) + +// non-depinject users +moduleManager.RegisterLegacyAminoCodec(legacyAmino) +moduleManager.RegisterInterfaces(interfaceRegistry) + +``` + ### Registering non app wiring modules It is possible to combine app wiring / depinject enabled modules with non app wiring modules. From f3eb0e3759adf1129e8917965ce9f2e6c3657086 Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Thu, 12 Sep 2024 12:09:05 +0300 Subject: [PATCH 2/8] chore(baseapp/docs): changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e74e876f31..985e219ab9dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Improvements +* (baseapp) [#21680](https://github.com/cosmos/cosmos-sdk/pull/21680) Add documentation on how to change the default genesis. + ### Bug Fixes * (baseapp) [#21256](https://github.com/cosmos/cosmos-sdk/pull/21256) Halt height will not commit the block indicated, meaning that if halt-height is set to 10, only blocks until 9 (included) will be committed. This is to go back to the original behavior before a change was introduced in v0.50.0. From 07cd3eeed517d19c8a8841de56d8f6d6372c51cf Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Thu, 12 Sep 2024 19:24:07 +0300 Subject: [PATCH 3/8] fix(baseapp/docs): rename struct --- docs/build/building-apps/01-app-go-di.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/build/building-apps/01-app-go-di.md b/docs/build/building-apps/01-app-go-di.md index 3f9288be4af6..d0408bc03cef 100644 --- a/docs/build/building-apps/01-app-go-di.md +++ b/docs/build/building-apps/01-app-go-di.md @@ -127,12 +127,12 @@ Previously, the `ModuleBasics` was a global variable that was used to register a Example ( staking ): ```go -type StakingModule struct { +type CustomStakingModule struct { staking.AppModuleBasic } // DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. -func (StakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { +func (CustomStakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { params := stakingtypes.DefaultParams() params.BondDenom = "mydenom" @@ -143,7 +143,7 @@ func (StakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { // option 1: using the previously removed global variable ModuleBasics = module.NewBasicManager( - StakingModule{}, // wrapped staking module + CustomStakingModule{}, // wrapped staking module auth.AppModuleBasic{}, distr.AppModuleBasic{}, From d2d22ce17281c3dee8a8dfe7cd7f38457e3e9950 Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Thu, 12 Sep 2024 19:25:30 +0300 Subject: [PATCH 4/8] chore(baseapp/docs): revert changelog --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 985e219ab9dd..54e74e876f31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,8 +46,6 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Improvements -* (baseapp) [#21680](https://github.com/cosmos/cosmos-sdk/pull/21680) Add documentation on how to change the default genesis. - ### Bug Fixes * (baseapp) [#21256](https://github.com/cosmos/cosmos-sdk/pull/21256) Halt height will not commit the block indicated, meaning that if halt-height is set to 10, only blocks until 9 (included) will be committed. This is to go back to the original behavior before a change was introduced in v0.50.0. From 83b248a233c5ee90b38d11499d8434ff8d20da0e Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Fri, 13 Sep 2024 09:52:44 +0300 Subject: [PATCH 5/8] fix(baseapp/docs): create new section --- docs/build/building-apps/01-app-go-di.md | 102 ------------------ docs/build/building-apps/06-app-go-genesis.md | 54 ++++++++++ 2 files changed, 54 insertions(+), 102 deletions(-) create mode 100644 docs/build/building-apps/06-app-go-genesis.md diff --git a/docs/build/building-apps/01-app-go-di.md b/docs/build/building-apps/01-app-go-di.md index d0408bc03cef..d77899d26e6d 100644 --- a/docs/build/building-apps/01-app-go-di.md +++ b/docs/build/building-apps/01-app-go-di.md @@ -118,108 +118,6 @@ More information on how work `depinject.Configs` and `depinject.Supply` can be f https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/simapp/app_v2.go#L114-L146 ``` -### Modifying the `DefaultGenesis` - -It is possible to modify the DefaultGenesis parameters for modules by wrapping the module and - -Previously, the `ModuleBasics` was a global variable that was used to register all modules' `AppModuleBasic` implementation. - -Example ( staking ): - -```go -type CustomStakingModule struct { - staking.AppModuleBasic -} - -// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. -func (CustomStakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { - params := stakingtypes.DefaultParams() - params.BondDenom = "mydenom" - - return cdc.MustMarshalJSON(&stakingtypes.GenesisState{ - Params: params, - }) -} - -// option 1: using the previously removed global variable -ModuleBasics = module.NewBasicManager( - CustomStakingModule{}, // wrapped staking module - auth.AppModuleBasic{}, - - distr.AppModuleBasic{}, - params.AppModuleBasic{}, - feegrantmodule.AppModuleBasic{}, - authzmodule.AppModuleBasic{}, - ibc.AppModuleBasic{}, - upgrade.AppModuleBasic{}, - evidence.AppModuleBasic{}, - ... - ) - -// option 2: the basic module manager can be now created from the module manager -app.BasicModuleManager = module.NewBasicManagerFromManager( - app.moduleManager, - // override staking AppModule - map[string]module.AppModuleBasic{ - stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}}, - }, -) - -// option 3: using depinject -depinject.Supply( - // supply custom module basics - map[string]module.AppModuleBasic{ - stakingtypes.ModuleName: CustomStakingModule{AppModuleBasic: &sdkstaking.AppModuleBasic{}}, - }, -) - -``` -The basic module manager has been deleted. It was not necessary anymore and was simplified to use the `module.Manager` directly. - -Example ( staking ) : - -```go -type CustomStakingModule struct { - staking.AppModule - cdc codec.Codec -} - -// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. -func (cm CustomStakingModule) DefaultGenesis() json.RawMessage { - params := stakingtypes.DefaultParams() - params.BondDenom = "mydenom" - - return cm.cdc.MustMarshalJSON(&stakingtypes.GenesisState{ - Params: params, - }) -} - -// option 1: use new module manager -moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ - stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, - // other modules ... -}) - -// option 2: override previous module manager -oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule) -moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ - AppModule: oldStakingModule, - cdc: appCodec, -} - - -// depinject users -depinject.Inject( - // ... - &moduleManager, - ) - -// non-depinject users -moduleManager.RegisterLegacyAminoCodec(legacyAmino) -moduleManager.RegisterInterfaces(interfaceRegistry) - -``` - ### Registering non app wiring modules It is possible to combine app wiring / depinject enabled modules with non app wiring modules. diff --git a/docs/build/building-apps/06-app-go-genesis.md b/docs/build/building-apps/06-app-go-genesis.md new file mode 100644 index 000000000000..eeebead97cec --- /dev/null +++ b/docs/build/building-apps/06-app-go-genesis.md @@ -0,0 +1,54 @@ +--- +sidebar_position: 1 +--- + + +### Modifying the `DefaultGenesis` + +It is possible to modify the DefaultGenesis parameters for modules by wrapping the module, providing it to the `*module.Manager` and injecting it with `depinject`. + + +Example ( staking ) : + +```go +type CustomStakingModule struct { + staking.AppModule + cdc codec.Codec +} + +// DefaultGenesis will override the Staking module DefaultGenesis AppModuleBasic method. +func (cm CustomStakingModule) DefaultGenesis() json.RawMessage { + params := stakingtypes.DefaultParams() + params.BondDenom = "mydenom" + + return cm.cdc.MustMarshalJSON(&stakingtypes.GenesisState{ + Params: params, + }) +} + +// option 1: use new module manager +moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ + stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, + // other modules ... +}) + +// option 2: override previous module manager +oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule) +moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ + AppModule: oldStakingModule, + cdc: appCodec, +} + + +// depinject users +depinject.Inject( + // ... + &moduleManager, + ) + +// non-depinject users +moduleManager.RegisterLegacyAminoCodec(legacyAmino) +moduleManager.RegisterInterfaces(interfaceRegistry) + +``` + From 89fe2cf6051da43ae1f36ae25e6eb4c2fbde9e7a Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Tue, 24 Sep 2024 21:29:37 +0300 Subject: [PATCH 6/8] fix(baseapp/docs): update docs --- docs/build/building-apps/06-app-go-genesis.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/build/building-apps/06-app-go-genesis.md b/docs/build/building-apps/06-app-go-genesis.md index eeebead97cec..841f8903fc97 100644 --- a/docs/build/building-apps/06-app-go-genesis.md +++ b/docs/build/building-apps/06-app-go-genesis.md @@ -26,13 +26,18 @@ func (cm CustomStakingModule) DefaultGenesis() json.RawMessage { }) } -// option 1: use new module manager +// option 1 ( for non depinject users ): use new module manager moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, // other modules ... }) -// option 2: override previous module manager +// option 2 ( for depinject users ): override previous module manager +depinject.Inject( +// ... provider/invoker/supplier +&moduleManager, +) + oldStakingModule,_ := moduleManager.Modules()[stakingtypes.ModuleName].(staking.AppModule) moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ AppModule: oldStakingModule, @@ -40,15 +45,7 @@ moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ } -// depinject users -depinject.Inject( - // ... - &moduleManager, - ) -// non-depinject users -moduleManager.RegisterLegacyAminoCodec(legacyAmino) -moduleManager.RegisterInterfaces(interfaceRegistry) ``` From 5c0e03af1f72fb311579639885cf6e49d39eb90b Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Wed, 25 Sep 2024 10:05:00 +0300 Subject: [PATCH 7/8] fix(baseapp/docs): update example --- docs/build/building-apps/06-app-go-genesis.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/docs/build/building-apps/06-app-go-genesis.md b/docs/build/building-apps/06-app-go-genesis.md index 841f8903fc97..57144b60213a 100644 --- a/docs/build/building-apps/06-app-go-genesis.md +++ b/docs/build/building-apps/06-app-go-genesis.md @@ -26,13 +26,7 @@ func (cm CustomStakingModule) DefaultGenesis() json.RawMessage { }) } -// option 1 ( for non depinject users ): use new module manager -moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ - stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, - // other modules ... -}) - -// option 2 ( for depinject users ): override previous module manager +// option 1 ( for depinject users ): override previous module manager depinject.Inject( // ... provider/invoker/supplier &moduleManager, @@ -44,8 +38,12 @@ moduleManager.Modules()[stakingtypes.ModuleName] = CustomStakingModule{ cdc: appCodec, } +// option 2 ( for non depinject users ): use new module manager +moduleManager := module.NewManagerFromMap(map[string]appmodule.AppModule{ +stakingtypes.ModuleName: CustomStakingModule{cdc: appCodec, AppModule: staking.NewAppModule(...)}, +// other modules ... +}) - - +// set the module manager +app.ModuleManager = moduleManager ``` - From fcc3afa3762ba205e786dff5282425c06b1cc544 Mon Sep 17 00:00:00 2001 From: Eric Mokaya Date: Wed, 25 Sep 2024 10:14:29 +0300 Subject: [PATCH 8/8] fix(baseapp/docs): formatting --- docs/build/building-apps/06-app-go-genesis.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/build/building-apps/06-app-go-genesis.md b/docs/build/building-apps/06-app-go-genesis.md index 57144b60213a..b9902858d7fe 100644 --- a/docs/build/building-apps/06-app-go-genesis.md +++ b/docs/build/building-apps/06-app-go-genesis.md @@ -2,12 +2,10 @@ sidebar_position: 1 --- - ### Modifying the `DefaultGenesis` It is possible to modify the DefaultGenesis parameters for modules by wrapping the module, providing it to the `*module.Manager` and injecting it with `depinject`. - Example ( staking ) : ```go