Configure Genesis State
Genesis configuration defines the initial state for storage items such as accounts, balances, genesis for custom pallets, and more. This guide demonstrates how to configure the genesis state for a pallet with the following storage items:
A
SingleValue<T>
type for a singleStorageValue
storage item.An
AccountMap<T>
type for simple single keyStorageMap
storage item.
Steps preview
Add the storage items in the pallet.
Add the genesis configuration macros in the pallet.
Set initial values in the chain specification.
Add storage items to the pallet
Open the
src//lib.rs
file for your pallet in a text editor.Add the
StorageValue
storage item.For example:
Add the
StorageMap
storage item for a map that has enumerable entries.For example:
Add genesis configuration macros
The GenesisConfig
code should go after your storage items.
Add the
#[pallet::genesis_config]
attribute macro and define theGenesisConfig
struct for the storage items to initialize.Set the default value for the
GenesisConfig
struct.Add the
#[pallet::genesis_build]
attribute macro and implement theGenesisBuild
trait.The
#[pallet::genesis_build]
macro allows you to execute some logic to define how theGenesisConfig
struct places something in storage.
Set initial values
After you configure the storage items and genesis configuration in your pallet, you can specify the values you want to set for the storage items in the genesis state of the chain. In this example, assume that the construct_runtime!
macro for runtime refers to PalletSomething
as the pallet name and pallet_something
as the path to the pallet.
Open the
node/chain_spec.rs
file in a text editor.Verify the
use node_template_runtime::BalanceConfig;
imported at the top of ourchain_spec.rs
file.Create a constant value of type
T::Balance
to be stored in<SingleValue<T>>
(inside thetestnet_genesis
method).Create a vector of accounts to initialize
<AccountMap<T>>
with inside thetestnet_genesis
method.Add the pallet to the
GenesisConfig
clause in thetestnet_genesis
function.The convention is to use lowercase spelling of the name of your pallet in
runtime/src/lib.rs
insideconstruct_runtime!
. For pallets declared asCamelCase
for example, the convention is to refer to it ascamel_case
in thetestnet_genesis
function.For this example pallet, the code looks like this:
This sample code maps each account from
accounts_to_map
to an amount equal toVALUE
. This pattern is very similar to theGenesisConfig
for the Balances pallet.
Example
Last updated