From 296c57686eb996d0fafcc2305067fbc5b9373365 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 25 Jun 2024 13:15:56 +0200 Subject: [PATCH] refactor: simplify comments in app.go and enable recommended option (#4203) * refactor: simplify comments in app.go and enable recommended option * updates --------- Co-authored-by: Danilo Pantani --- .../app/files-consumer/app/app.go.plush | 108 ++++-------------- .../app/files-minimal/app/app.go.plush | 93 +++------------ ignite/templates/app/files/app/app.go.plush | 94 +++------------ 3 files changed, 52 insertions(+), 243 deletions(-) diff --git a/ignite/templates/app/files-consumer/app/app.go.plush b/ignite/templates/app/files-consumer/app/app.go.plush index 3fa6378731..c9186b9f87 100644 --- a/ignite/templates/app/files-consumer/app/app.go.plush +++ b/ignite/templates/app/files-consumer/app/app.go.plush @@ -146,7 +146,7 @@ func init() { func AppConfig() depinject.Config { return depinject.Configs( appConfig, - // Loads the app config from a YAML file. + // Alternatively, load the app config from a YAML file. // appconfig.LoadYAML(AppConfigYAML), depinject.Supply( // supply custom module basics @@ -175,57 +175,22 @@ func New( appConfig = depinject.Configs( AppConfig(), depinject.Supply( - // Supply the application options - appOpts, - // Supply with IBC keeper getter for the IBC modules with App Wiring. - // The IBC Keeper cannot be passed because it has not been initiated yet. - // Passing the getter, the app IBC Keeper will always be accessible. - // This needs to be removed after IBC supports App Wiring. - app.GetIBCKeeper, - app.GetCapabilityScopedKeeper, + appOpts, // supply app options + logger, // supply logger + // Supply with IBC keeper getter for the IBC modules with App Wiring. + // The IBC Keeper cannot be passed because it has not been initiated yet. + // Passing the getter, the app IBC Keeper will always be accessible. + // This needs to be removed after IBC supports App Wiring. + app.GetIBCKeeper, + app.GetCapabilityScopedKeeper, // Supply the consumer keeper for the consumer module &app.ConsumerKeeper, - // Supply the logger - logger, - - // ADVANCED CONFIGURATION - // - // AUTH - // - // For providing a custom function required in auth to generate custom account types - // add it below. By default the auth module uses simulation.RandomGenesisAccounts. - // - // authtypes.RandomGenesisAccountsFn(simulation.RandomGenesisAccounts), - - // For providing a custom a base account type add it below. - // By default the auth module uses authtypes.ProtoBaseAccount(). - // - // func() sdk.AccountI { return authtypes.ProtoBaseAccount() }, - // - // For providing a different address codec, add it below. - // By default the auth module uses a Bech32 address codec, - // with the prefix defined in the auth module configuration. - // - // func() address.Codec { return <- custom address codec type -> } - - // - // STAKING - // - // For provinding a different validator and consensus address codec, add it below. - // By default the staking module uses the bech32 prefix provided in the auth config, - // and appends "valoper" and "valcons" for validator and consensus addresses respectively. - // When providing a custom address codec in auth, custom address codecs must be provided here as well. - // - // func() runtime.ValidatorAddressCodec { return <- custom validator address codec type -> } - // func() runtime.ConsensusAddressCodec { return <- custom consensus address codec type -> } - - // - // MINT - // - - // For providing a custom inflation function for x/mint add here your - // custom function that implements the minttypes.InflationCalculationFn - // interface. + + // here alternative options can be supplied to the DI container. + // those options can be used f.e to override the default behavior of some modules. + // for instance supplying a custom address codec for not using bech32 addresses. + // read the depinject documentation and depinject module wiring for more information + // on available options and how to use them. ), ) ) @@ -254,41 +219,14 @@ func New( panic(err) } - // Below we could construct and set an application specific mempool and - // ABCI 1.0 PrepareProposal and ProcessProposal handlers. These defaults are - // already set in the SDK's BaseApp, this shows an example of how to override - // them. - // - // Example: - // - // app.App = appBuilder.Build(...) - // nonceMempool := mempool.NewSenderNonceMempool() - // abciPropHandler := NewDefaultProposalHandler(nonceMempool, app.App.BaseApp) - // - // app.App.BaseApp.SetMempool(nonceMempool) - // app.App.BaseApp.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // app.App.BaseApp.SetProcessProposal(abciPropHandler.ProcessProposalHandler()) - // - // Alternatively, you can construct BaseApp options, append those to - // baseAppOptions and pass them to the appBuilder. - // - // Example: - // - // prepareOpt = func(app *baseapp.BaseApp) { - // abciPropHandler := baseapp.NewDefaultProposalHandler(nonceMempool, app) - // app.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // } - // baseAppOptions = append(baseAppOptions, prepareOpt) - // - // create and set vote extension handler - // voteExtOp := func(bApp *baseapp.BaseApp) { - // voteExtHandler := NewVoteExtensionHandler() - // voteExtHandler.SetHandlers(bApp) - // } + // add to default baseapp options + // enable optimistic execution + baseAppOptions = append(baseAppOptions, baseapp.SetOptimisticExecution()) + // build app app.App = appBuilder.Build(db, traceStore, baseAppOptions...) - // Register legacy modules + // register legacy modules if err := app.registerIBCModules(appOpts); err != nil { return nil, err } @@ -303,8 +241,6 @@ func New( app.ModuleManager.RegisterInvariants(app.CrisisKeeper) // create the simulation manager and define the order of the modules for deterministic simulations - // - // NOTE: this is not required apps that don't use the simulator for fuzz testing transactions overrideModules := map[string]module.AppModuleSimulation{ authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)), } @@ -312,7 +248,7 @@ func New( app.sm.RegisterStoreDecoders() - // TODO theres probably a better way to SetAnteHandler with app wiring + // overwrite default antehandlers anteHandler, err := NewAnteHandler( HandlerOptions{ HandlerOptions: ante.HandlerOptions{ @@ -335,7 +271,7 @@ func New( // A custom InitChainer sets if extra pre-init-genesis logic is required. // This is necessary for manually registered modules that do not support app wiring. - // Manually set the module version map as shown below. + // Manually set the module version map as shown below. // The upgrade module will automatically handle de-duplication of the module version map. app.SetInitChainer(func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) { if err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap()); err != nil { diff --git a/ignite/templates/app/files-minimal/app/app.go.plush b/ignite/templates/app/files-minimal/app/app.go.plush index b1995d6e2e..e5bf30d727 100644 --- a/ignite/templates/app/files-minimal/app/app.go.plush +++ b/ignite/templates/app/files-minimal/app/app.go.plush @@ -91,7 +91,7 @@ func init() { func AppConfig() depinject.Config { return depinject.Configs( appConfig, - // Loads the app config from a YAML file. + // Alternatively, load the app config from a YAML file. // appconfig.LoadYAML(AppConfigYAML), depinject.Supply( // supply custom module basics @@ -120,49 +120,14 @@ func New( appConfig = depinject.Configs( AppConfig(), depinject.Supply( - // Supply the application options - appOpts, - // Supply the logger - logger, - - // ADVANCED CONFIGURATION - // - // AUTH - // - // For providing a custom function required in auth to generate custom account types - // add it below. By default the auth module uses simulation.RandomGenesisAccounts. - // - // authtypes.RandomGenesisAccountsFn(simulation.RandomGenesisAccounts), - // - // For providing a custom a base account type add it below. - // By default the auth module uses authtypes.ProtoBaseAccount(). - // - // func() sdk.AccountI { return authtypes.ProtoBaseAccount() }, - // - // For providing a different address codec, add it below. - // By default the auth module uses a Bech32 address codec, - // with the prefix defined in the auth module configuration. - // - // func() address.Codec { return <- custom address codec type -> } - - // - // STAKING - // - // For provinding a different validator and consensus address codec, add it below. - // By default the staking module uses the bech32 prefix provided in the auth config, - // and appends "valoper" and "valcons" for validator and consensus addresses respectively. - // When providing a custom address codec in auth, custom address codecs must be provided here as well. - // - // func() runtime.ValidatorAddressCodec { return <- custom validator address codec type -> } - // func() runtime.ConsensusAddressCodec { return <- custom consensus address codec type -> } - - // - // MINT - // - - // For providing a custom inflation function for x/mint add here your - // custom function that implements the minttypes.InflationCalculationFn - // interface. + appOpts, // supply app options + logger, // supply logger + + // here alternative options can be supplied to the DI container. + // those options can be used f.e to override the default behavior of some modules. + // for instance supplying a custom address codec for not using bech32 addresses. + // read the depinject documentation and depinject module wiring for more information + // on available options and how to use them. ), ) ) @@ -183,38 +148,11 @@ func New( panic(err) } - // Below we could construct and set an application specific mempool and - // ABCI 1.0 PrepareProposal and ProcessProposal handlers. These defaults are - // already set in the SDK's BaseApp, this shows an example of how to override - // them. - // - // Example: - // - // app.App = appBuilder.Build(...) - // nonceMempool := mempool.NewSenderNonceMempool() - // abciPropHandler := NewDefaultProposalHandler(nonceMempool, app.App.BaseApp) - // - // app.App.BaseApp.SetMempool(nonceMempool) - // app.App.BaseApp.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // app.App.BaseApp.SetProcessProposal(abciPropHandler.ProcessProposalHandler()) - // - // Alternatively, you can construct BaseApp options, append those to - // baseAppOptions and pass them to the appBuilder. - // - // Example: - // - // prepareOpt = func(app *baseapp.BaseApp) { - // abciPropHandler := baseapp.NewDefaultProposalHandler(nonceMempool, app) - // app.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // } - // baseAppOptions = append(baseAppOptions, prepareOpt) - // - // create and set vote extension handler - // voteExtOp := func(bApp *baseapp.BaseApp) { - // voteExtHandler := NewVoteExtensionHandler() - // voteExtHandler.SetHandlers(bApp) - // } + // add to default baseapp options + // enable optimistic execution + baseAppOptions = append(baseAppOptions, baseapp.SetOptimisticExecution()) + // build app app.App = appBuilder.Build(db, traceStore, baseAppOptions...) // register streaming services @@ -222,9 +160,8 @@ func New( return nil, err } - /**** Module Options ****/ - overrideModules := make(map[string]module.AppModuleSimulation) - app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules) + // create the simulation manager and define the order of the modules for deterministic simulations + app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, make(map[string]module.AppModuleSimulation)) app.sm.RegisterStoreDecoders() // A custom InitChainer can be set if extra pre-init-genesis logic is required. diff --git a/ignite/templates/app/files/app/app.go.plush b/ignite/templates/app/files/app/app.go.plush index 4f55168cd0..cd0f61260f 100644 --- a/ignite/templates/app/files/app/app.go.plush +++ b/ignite/templates/app/files/app/app.go.plush @@ -170,7 +170,7 @@ func getGovProposalHandlers() []govclient.ProposalHandler { func AppConfig() depinject.Config { return depinject.Configs( appConfig, - // Loads the app config from a YAML file. + // Alternatively, load the app config from a YAML file. // appconfig.LoadYAML(AppConfigYAML), depinject.Supply( // supply custom module basics @@ -200,55 +200,20 @@ func New( appConfig = depinject.Configs( AppConfig(), depinject.Supply( - // Supply the application options - appOpts, + appOpts, // supply app options + logger, // supply logger // Supply with IBC keeper getter for the IBC modules with App Wiring. // The IBC Keeper cannot be passed because it has not been initiated yet. // Passing the getter, the app IBC Keeper will always be accessible. // This needs to be removed after IBC supports App Wiring. app.GetIBCKeeper, app.GetCapabilityScopedKeeper, - // Supply the logger - logger, - - // ADVANCED CONFIGURATION - // - // AUTH - // - // For providing a custom function required in auth to generate custom account types - // add it below. By default the auth module uses simulation.RandomGenesisAccounts. - // - // authtypes.RandomGenesisAccountsFn(simulation.RandomGenesisAccounts), - // - // For providing a custom a base account type add it below. - // By default the auth module uses authtypes.ProtoBaseAccount(). - // - // func() sdk.AccountI { return authtypes.ProtoBaseAccount() }, - // - // For providing a different address codec, add it below. - // By default the auth module uses a Bech32 address codec, - // with the prefix defined in the auth module configuration. - // - // func() address.Codec { return <- custom address codec type -> } - - // - // STAKING - // - // For provinding a different validator and consensus address codec, add it below. - // By default the staking module uses the bech32 prefix provided in the auth config, - // and appends "valoper" and "valcons" for validator and consensus addresses respectively. - // When providing a custom address codec in auth, custom address codecs must be provided here as well. - // - // func() runtime.ValidatorAddressCodec { return <- custom validator address codec type -> } - // func() runtime.ConsensusAddressCodec { return <- custom consensus address codec type -> } - - // - // MINT - // - - // For providing a custom inflation function for x/mint add here your - // custom function that implements the minttypes.InflationCalculationFn - // interface. + + // here alternative options can be supplied to the DI container. + // those options can be used f.e to override the default behavior of some modules. + // for instance supplying a custom address codec for not using bech32 addresses. + // read the depinject documentation and depinject module wiring for more information + // on available options and how to use them. ), ) ) @@ -281,41 +246,14 @@ func New( panic(err) } - // Below we could construct and set an application specific mempool and - // ABCI 1.0 PrepareProposal and ProcessProposal handlers. These defaults are - // already set in the SDK's BaseApp, this shows an example of how to override - // them. - // - // Example: - // - // app.App = appBuilder.Build(...) - // nonceMempool := mempool.NewSenderNonceMempool() - // abciPropHandler := NewDefaultProposalHandler(nonceMempool, app.App.BaseApp) - // - // app.App.BaseApp.SetMempool(nonceMempool) - // app.App.BaseApp.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // app.App.BaseApp.SetProcessProposal(abciPropHandler.ProcessProposalHandler()) - // - // Alternatively, you can construct BaseApp options, append those to - // baseAppOptions and pass them to the appBuilder. - // - // Example: - // - // prepareOpt = func(app *baseapp.BaseApp) { - // abciPropHandler := baseapp.NewDefaultProposalHandler(nonceMempool, app) - // app.SetPrepareProposal(abciPropHandler.PrepareProposalHandler()) - // } - // baseAppOptions = append(baseAppOptions, prepareOpt) - // - // create and set vote extension handler - // voteExtOp := func(bApp *baseapp.BaseApp) { - // voteExtHandler := NewVoteExtensionHandler() - // voteExtHandler.SetHandlers(bApp) - // } + // add to default baseapp options + // enable optimistic execution + baseAppOptions = append(baseAppOptions, baseapp.SetOptimisticExecution()) + // build app app.App = appBuilder.Build(db, traceStore, baseAppOptions...) - // Register legacy modules + // register legacy modules if err := app.registerIBCModules(appOpts); err != nil { return nil, err } @@ -330,8 +268,6 @@ func New( app.ModuleManager.RegisterInvariants(app.CrisisKeeper) // create the simulation manager and define the order of the modules for deterministic simulations - // - // NOTE: this is not required apps that don't use the simulator for fuzz testing transactions overrideModules := map[string]module.AppModuleSimulation{ authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)), } @@ -340,7 +276,7 @@ func New( // A custom InitChainer sets if extra pre-init-genesis logic is required. // This is necessary for manually registered modules that do not support app wiring. - // Manually set the module version map as shown below. + // Manually set the module version map as shown below. // The upgrade module will automatically handle de-duplication of the module version map. app.SetInitChainer(func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) { if err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap()); err != nil {