polkadot_sdk_docs/reference_docs/chain_spec_genesis.rs
1//! # What is a chain specification
2//!
3//! A chain specification file defines the set of properties that are required to run the node as
4//! part of the chain. The chain specification consists of two main parts:
5//! - initial state of the runtime,
6//! - network / logical properties of the chain, the most important property being the list of
7//! bootnodes.
8//!
9//! This document describes how the initial state is handled in pallets and runtime, and how to
10//! interact with the runtime in order to build the genesis state.
11//!
12//! For more information on chain specification and its properties, refer to
13//! [`sc_chain_spec#from-initial-state-to-raw-genesis`].
14//!
15//! The initial genesis state can be provided in the following formats:
16//! - full
17//! - patch
18//! - raw
19//!
20//! Each of the formats is explained in [_chain-spec-format_][`sc_chain_spec#chain-spec-formats`].
21//!
22//!
23//! # `GenesisConfig` for `pallet`
24//!
25//! Every frame pallet may have its initial state which is defined by the `GenesisConfig` internal
26//! struct. It is a regular Rust struct, annotated with the [`pallet::genesis_config`] attribute.
27#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar_GenesisConfig)]
28//! The struct shall be defined within the pallet `mod`, as in the following code:
29#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar)]
30//! The initial state conveyed in the `GenesisConfig` struct is transformed into state storage
31//! items by means of the [`BuildGenesisConfig`] trait, which shall be implemented for the pallet's
32//! `GenesisConfig` struct. The [`pallet::genesis_build`] attribute shall be attached to the `impl`
33//! block:
34#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar_build)]
35//! `GenesisConfig` may also contain more complicated types, including nested structs or enums, as
36//! in the example for `pallet_foo`:
37#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_foo_GenesisConfig)]
38//! Note that [`serde`] attributes can be used to control how the data
39//! structures are stored into JSON. In the following example, the [`sp_core::bytes`] function is
40//! used to serialize the `values` field.
41#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", SomeFooData2)]
42//! Please note that fields of `GenesisConfig` may not be directly mapped to storage items. In the
43//! following example, the initial struct fields are used to compute (sum) the value that will be
44//! stored in the state as `ProcessedEnumValue`:
45#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_foo_build)]
46//! # `GenesisConfig` for `runtimes`
47//!
48//! The runtime genesis config struct consists of configs for every pallet. For the [_demonstration
49//! runtime_][`chain_spec_guide_runtime`] used in this guide, it consists of `SystemConfig`,
50//! `BarConfig`, and `FooConfig`. This structure was automatically generated by a macro and it can
51//! be sneak-peeked here: [`RuntimeGenesisConfig`]. For further reading on generated runtime
52//! types, refer to [`frame_runtime_types`].
53//!
54//! The macro automatically adds an attribute that renames all the fields to [`camelCase`]. It is a
55//! good practice to add it to nested structures too, to have the naming of the JSON keys consistent
56//! across the chain-spec file.
57//!
58//! ## `Default` for `GenesisConfig`
59//!
60//! `GenesisConfig` of all pallets must implement the `Default` trait. These are aggregated into
61//! the runtime's `RuntimeGenesisConfig`'s `Default`.
62//!
63//! The default value of `RuntimeGenesisConfig` can be queried by the [`GenesisBuilder::get_preset`]
64//! function provided by the runtime with `id:None`.
65//!
66//! A default value for `RuntimeGenesisConfig` usually is not operational. This is because for some
67//! pallets it is not possible to define good defaults (e.g. an initial set of authorities).
68//!
69//! A default value is a base upon which a patch for `GenesisConfig` is applied. A good description
70//! of how it exactly works is provided in [`get_storage_for_patch`] (and also in
71//! [`GenesisBuilder::get_preset`]). A patch can be provided as an external file (manually created)
72//! or as a built-in runtime preset. More info on presets is in the material to follow.
73//!
74//! ## Implementing `GenesisBuilder` for runtime
75//!
76//! The runtime exposes a dedicated runtime API for interacting with its genesis config:
77//! [`sp_genesis_builder::GenesisBuilder`]. The implementation shall be provided within
78//! the [`sp_api::impl_runtime_apis`] macro, typically making use of some helpers provided:
79//! [`build_state`], [`get_preset`].
80//! A typical implementation of [`sp_genesis_builder::GenesisBuilder`] looks as follows:
81#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/runtime.rs", runtime_impl)]
82//! Please note that two functions are customized: `preset_names` and `get_preset`. The first one
83//! just provides a `Vec` of the names of supported presets, while the latter delegates the call
84//! to a function that maps the name to an actual preset:
85//! [`chain_spec_guide_runtime::presets::get_builtin_preset`]
86#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", get_builtin_preset)]
87//! ## Genesis state presets for runtime
88//!
89//! The runtime may provide many flavors of initial genesis state. This may be useful for predefined
90//! testing networks, local development, or CI integration tests. Predefined genesis state may
91//! contain a list of pre-funded accounts, predefined authorities for consensus, sudo key, and many
92//! others useful for testing.
93//!
94//! Internally, presets can be provided in a number of ways:
95//! - using [`build_struct_json_patch`] macro (**recommended**):
96#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_2)]
97//! - JSON using runtime types to serialize values:
98#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_3)]
99//! - JSON in string form:
100#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_1)]
101//! It is worth noting that a preset does not have to be the full `RuntimeGenesisConfig`, in that
102//! sense that it does not have to contain all the keys of the struct. The preset is actually a JSON
103//! patch that will be merged with the default value of `RuntimeGenesisConfig`. This approach should
104//! simplify maintenance of built-in presets. The following example illustrates a runtime genesis
105//! config patch with a single key built using [`build_struct_json_patch`] macro:
106#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_4)]
107//! This results in the following JSON blob:
108#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", preset_4_json)]
109//! ## Note on the importance of testing presets
110//!
111//! It is recommended to always test presets by adding tests that convert the preset into the
112//! raw storage. Converting to raw storage involves the deserialization of the provided JSON blob,
113//! which enforces the verification of the preset. The following code shows one of the approaches
114//! that can be taken for testing:
115#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", check_presets)]
116//! ## Note on the importance of using the `deny_unknown_fields` attribute
117//!
118//! It is worth noting that when manually building preset JSON blobs it is easy to make a
119//! hard-to-spot mistake, as in the following example ([`FooStruct`] does not contain `fieldC`):
120#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_invalid)]
121//! Even though `preset_invalid` contains a key that does not exist, the deserialization of the JSON
122//! blob does not fail. The misspelling is silently ignored due to the lack of the
123//! [`deny_unknown_fields`] attribute on the [`FooStruct`] struct, which is internally used in
124//! `GenesisConfig`.
125#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", invalid_preset_works)]
126//! To avoid this problem [`build_struct_json_patch`] macro shall be used whenever possible (it
127//! internally instantiates the struct before serializang it JSON blob, so all unknown fields shall
128//! be caught at compilation time).
129//!
130//! ## Runtime `GenesisConfig` raw format
131//!
132//! A raw format of genesis config contains just the state's keys and values as they are stored in
133//! the storage. This format is used to directly initialize the genesis storage. This format is
134//! useful for long-term running chains, where the `GenesisConfig` structure for pallets may be
135//! evolving over time. The JSON representation created at some point in time may no longer be
136//! deserializable in the future, making a chain specification useless. The raw format is
137//! recommended for production chains.
138//!
139//! For a detailed description of how the raw format is built, please refer to
140//! [_chain-spec-raw-genesis_][`sc_chain_spec#from-initial-state-to-raw-genesis`]. Plain and
141//! corresponding raw examples of chain-spec are given in
142//! [_chain-spec-examples_][`sc_chain_spec#json-chain-specification-example`].
143//! The [`chain_spec_builder`] util supports building the raw storage.
144//!
145//! # Interacting with the tool
146//!
147//! The [`chain_spec_builder`] util allows interaction with the runtime in order to list or display
148//! presets and build the chain specification file. It is possible to use the tool with the
149//! [_demonstration runtime_][`chain_spec_guide_runtime`]. To build the required packages, just run
150//! the following command:
151//!
152//! ```ignore
153//! cargo build -p staging-chain-spec-builder -p chain-spec-guide-runtime --release
154//! ```
155//!
156//! The `chain-spec-builder` util can also be installed with `cargo install`:
157//!
158//! ```ignore
159//! cargo install staging-chain-spec-builder
160//! cargo build -p chain-spec-guide-runtime --release
161//! ```
162//! Here are some examples in the form of rust tests:
163//! ## Listing available preset names:
164#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", cmd_list_presets)]
165//! ## Displaying preset with given name
166#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", cmd_get_preset)]
167//! ## Building a solo chain-spec (the default) using given preset
168#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", cmd_generate_chain_spec)]
169//! ## Building a parachain chain-spec using given preset
170#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", cmd_generate_para_chain_spec)]
171//! [`RuntimeGenesisConfig`]:
172//! chain_spec_guide_runtime::runtime::RuntimeGenesisConfig
173//! [`FooStruct`]:
174//! chain_spec_guide_runtime::pallets::FooStruct
175//! [`impl_runtime_apis`]: frame::runtime::prelude::impl_runtime_apis
176//! [`build_state`]: frame_support::genesis_builder_helper::build_state
177//! [`get_preset`]: frame_support::genesis_builder_helper::get_preset
178//! [`pallet::genesis_build`]: frame_support::pallet_macros::genesis_build
179//! [`pallet::genesis_config`]: frame_support::pallet_macros::genesis_config
180//! [`build_struct_json_patch`]: frame_support::build_struct_json_patch
181//! [`BuildGenesisConfig`]: frame_support::traits::BuildGenesisConfig
182//! [`serde`]: https://serde.rs/field-attrs.html
183//! [`get_storage_for_patch`]: sc_chain_spec::GenesisConfigBuilderRuntimeCaller::get_storage_for_patch
184//! [`GenesisBuilder::get_preset`]: sp_genesis_builder::GenesisBuilder::get_preset
185//! [`deny_unknown_fields`]: https://serde.rs/container-attrs.html#deny_unknown_fields
186//! [`camelCase`]: https://serde.rs/container-attrs.html#rename_all