runtime
package in the Cosmos SDK provides a flexible framework for configuring and managing blockchain applications. It serves as the foundation for creating modular blockchain applications using a declarative configuration approach.
Overview
The runtime package acts as a wrapper around theBaseApp
and ModuleManager
, offering a hybrid approach where applications can be configured both declaratively through configuration files and programmatically through traditional methods. It is a layer of abstraction between baseapp
and the application modules that simplifies the process of building a Cosmos SDK application.
Core Components
App Structure
The runtime App struct contains several key components:*runtime.App
struct to leverage the runtime module.
simapp/app_di.go
Configuration
The runtime module is configured using App Wiring. The main configuration object is theModule
message, which supports the following key settings:
app_name
: The name of the applicationbegin_blockers
: List of module names to call during BeginBlockend_blockers
: List of module names to call during EndBlockinit_genesis
: Order of module initialization during genesisexport_genesis
: Order for exporting module genesis datapre_blockers
: Modules to execute before block processing
runtime
in the next section.
Store Configuration
By default, the runtime module uses the module name as the store key. However it provides a flexible store key configuration through:override_store_keys
: Allows customizing module store keysskip_store_keys
: Specifies store keys to skip during keeper construction
Key Features
1. BaseApp and other Core SDK components integration
The runtime module integrates with theBaseApp
and other core SDK components to provide a seamless experience for developers.
The developer only needs to embed the runtime.App
struct in their application to leverage the runtime module. The configuration of the module manager and other core components is handled internally via the AppBuilder
.
2. Module Registration
Runtime has built-in support fordepinject
-enabled modules. Such modules can be registered through the configuration file (often named app_config.go
), with no additional code required.
simapp/app_config.go
RegisterModules
method. This is the primary integration point for modules not registered via configuration.
Even when using manual registration, the module should still be configured in the
Module
message in AppConfig.depinject
for module registration whenever possible.
3. Service Registration
Runtime registers all core services required by modules. These services includestore
, event manager
, context
, and logger
. Runtime ensures that services are scoped to their respective modules during the wiring process.
runtime/module.go
- AutoCLI Query Service
- Reflection Service
- Custom module services
4. Application Building
TheAppBuilder
type provides a structured way to build applications:
runtime/builder.go
- Configuration loading
- Module registration
- Service setup
- Store mounting
- Router configuration
AppBuilder.Build
to create a fully configured application (runtime.App
).
runtime/builder.go
Best Practices
- Module Order: Carefully consider the order of modules in begin_blockers, end_blockers, and pre_blockers.
- Store Keys: Use override_store_keys only when necessary to maintain clarity
- Genesis Order: Maintain correct initialization order in init_genesis
- Migration Management: Use order_migrations to control upgrade paths
Migration Considerations
When upgrading between versions:- Review the migration order specified in
order_migrations
- Ensure all required modules are included in the configuration
- Validate store key configurations
- Test the upgrade path thoroughly