Substrate

GenesisConfig Struct

The genesis configuration is used to initialize the state of storage items in the Substrate runtime at the first block.

Declaring Configurable Storage Items

In Substrate, a storage item can be initialized in the genesis configuration. To enable this, you add the config() parameter to the storage item declaration.

For example, in the SRML sudo module (srml-sudo), the storage item Key is declared with a config() parameter.

decl_storage! {
    trait Store for Module<T: Trait> as Sudo {
        Key get(key) config(): T::AccountId;
    }
}

The Module GenesisConfig Type

When the decl_storage! macro expands, it generates the GenesisConfig type, which includes a reference to each storage item declared with the config() parameter.

For example, for the SRML sudo module (srml-sudo), the following GenesisConfig type is generated.

pub struct GenesisConfig<T: Trait> {
    pub key: T::AccountId,
}

Each module that supports a genesis configuration will also have its GenesisConfig type aliased as <ModuleName>Config as part of the construct_runtime! macro expansion:

pub type SudoConfig = sudo::GenesisConfig<Runtime>;

The Outer GenesisConfig Struct

Once a storage item is declared with a genesis configuration, its value needs to be set. We do this through an outer GenesisConfig type, which is generated by the construct_runtime! macro:

pub struct GenesisConfig {
    pub system: Option<SystemConfig>,
    pub timestamp: Option<TimestampConfig>,
    pub consensus: Option<ConsensusConfig>,
    pub indices: Option<IndicesConfig>,
    pub balances: Option<BalancesConfig>,
    pub sudo: Option<SudoConfig>,
}

This outer type includes references to each of the modules' GenesisConfig types.

Setting the Genesis Configuration Values

Now that we have established the module and outer GenesisConfig types, we need to set their values.

You can find an example of this in Substrate's node-template within the chain_spec.rs file. For the SRML sudo module (srml-sudo), the following code snippet shows the genesis configuration of the storage item Key.

fn testnet_genesis(root_key: AccountId, ...) -> GenesisConfig {
    GenesisConfig {
        sudo: Some(SudoConfig 
        {
            key: root_key,
        }),
        ...
    }
}

When you start your node, the outer GenesisConfig that you specified will be used to set the initial values into storage. At that point, you can access and use those values just like you would any other runtime storage item.

Note: The GenesisConfig type is not available for the WASM runtime environment, but only available for the native environment. The configurable storage items are treated as pre-initialized by the WASM environment.

Exposing the GenesisConfig Type

The GenesisConfig type is not directly exposed through the Substrate metadata endpoint. Instead, the GenesisConfig is used to set an initial value for Storage items, so you can find the impact of this configuration by querying the storage.

GenesisConfig Struct


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.