sp_genesis_builder/lib.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19
20//! # Substrate genesis config builder.
21//!
22//! This crate contains [`GenesisBuilder`], a runtime-api to be implemented by runtimes, in order to
23//! express their genesis state.
24//!
25//! The overall flow of the methods in [`GenesisBuilder`] is as follows:
26//!
27//! 1. [`GenesisBuilder::preset_names`]: A runtime exposes a number of different
28//! `RuntimeGenesisConfig` variations, each of which is called a `preset`, and is identified by a
29//! [`PresetId`]. All runtimes are encouraged to expose at least [`DEV_RUNTIME_PRESET`] and
30//! [`LOCAL_TESTNET_RUNTIME_PRESET`] presets for consistency.
31//! 2. [`GenesisBuilder::get_preset`]: Given a `PresetId`, this the runtime returns the JSON blob
32//! representation of the `RuntimeGenesisConfig` for that preset. This JSON blob is often mixed
33//! into the broader `chain_spec`. If `None` is given, [`GenesisBuilder::get_preset`] provides a
34//! JSON represention of the default `RuntimeGenesisConfig` (by simply serializing the
35//! `RuntimeGenesisConfig::default()` value into JSON format). This is used as a base for
36//! applying patches / presets.
37
38//! 3. [`GenesisBuilder::build_state`]: Given a JSON blob, this method should deserialize it and
39//! enact it (using `frame_support::traits::BuildGenesisConfig` for Frame-based runtime),
40//! essentially writing it to the state.
41//!
42//! The first two flows are often done in between a runtime, and the `chain_spec_builder` binary.
43//! The latter is used when a new blockchain is launched to enact and store the genesis state. See
44//! the documentation of `chain_spec_builder` for more info.
45//!
46//! ## Patching
47//!
48//! The runtime may provide a number of partial predefined `RuntimeGenesisConfig` configurations in
49//! the form of patches which shall be applied on top of the default `RuntimeGenesisConfig`. The
50//! patch is a JSON blob, which essentially comprises the list of key-value pairs that are to be
51//! customized in the default runtime genesis config. These predefined configurations are referred
52//! to as presets.
53//!
54//! This allows the runtime to provide a number of predefined configs (e.g. for different testnets
55//! or development) without necessarily to leak the runtime types outside itself (e.g. node or
56//! chain-spec related tools).
57//!
58//! ## FRAME vs. non-FRAME
59//!
60//! For FRAME based runtimes [`GenesisBuilder`] provides means to interact with
61//! `RuntimeGenesisConfig`.
62//!
63//! For non-FRAME runtimes this interface is intended to build genesis state of the runtime basing
64//! on some input arbitrary bytes array. This documentation uses term `RuntimeGenesisConfig`, which
65//! for non-FRAME runtimes may be understood as the "runtime-side entity representing initial
66//! runtime genesis configuration". The representation of the preset is an arbitrary `Vec<u8>` and
67//! does not necessarily have to represent a JSON blob.
68//!
69//! ## Genesis Block State
70//!
71//! Providing externalities with an empty storage and putting `RuntimeGenesisConfig` into storage
72//! (by calling `build_state`) allows to construct the raw storage of `RuntimeGenesisConfig`
73//! which is the foundation for genesis block.
74
75extern crate alloc;
76use alloc::{string::String, vec::Vec};
77
78/// The result type alias, used in build methods. `Err` contains formatted error message.
79pub type Result = core::result::Result<(), String>;
80
81/// The type representing preset ID.
82pub type PresetId = String;
83
84/// The default `development` preset used to communicate with the runtime via
85/// [`GenesisBuilder`] interface.
86///
87/// (Recommended for testing with a single node, e.g., for benchmarking)
88pub const DEV_RUNTIME_PRESET: &'static str = "development";
89
90/// The default `local_testnet` preset used to communicate with the runtime via
91/// [`GenesisBuilder`] interface.
92///
93/// (Recommended for local testing with multiple nodes)
94pub const LOCAL_TESTNET_RUNTIME_PRESET: &'static str = "local_testnet";
95
96sp_api::decl_runtime_apis! {
97 /// API to interact with `RuntimeGenesisConfig` for the runtime
98 pub trait GenesisBuilder {
99 /// Build `RuntimeGenesisConfig` from a JSON blob not using any defaults and store it in the
100 /// storage.
101 ///
102 /// In the case of a FRAME-based runtime, this function deserializes the full
103 /// `RuntimeGenesisConfig` from the given JSON blob and puts it into the storage. If the
104 /// provided JSON blob is incorrect or incomplete or the deserialization fails, an error
105 /// is returned.
106 ///
107 /// Please note that provided JSON blob must contain all `RuntimeGenesisConfig` fields, no
108 /// defaults will be used.
109 fn build_state(json: Vec<u8>) -> Result;
110
111 /// Returns a JSON blob representation of the built-in `RuntimeGenesisConfig` identified by
112 /// `id`.
113 ///
114 /// If `id` is `None` the function should return JSON blob representation of the default
115 /// `RuntimeGenesisConfig` struct of the runtime. Implementation must provide default
116 /// `RuntimeGenesisConfig`.
117 ///
118 /// Otherwise function returns a JSON representation of the built-in, named
119 /// `RuntimeGenesisConfig` preset identified by `id`, or `None` if such preset does not
120 /// exist. Returned `Vec<u8>` contains bytes of JSON blob (patch) which comprises a list of
121 /// (potentially nested) key-value pairs that are intended for customizing the default
122 /// runtime genesis config. The patch shall be merged (rfc7386) with the JSON representation
123 /// of the default `RuntimeGenesisConfig` to create a comprehensive genesis config that can
124 /// be used in `build_state` method.
125 fn get_preset(id: &Option<PresetId>) -> Option<Vec<u8>>;
126
127 /// Returns a list of identifiers for available builtin `RuntimeGenesisConfig` presets.
128 ///
129 /// The presets from the list can be queried with [`GenesisBuilder::get_preset`] method. If
130 /// no named presets are provided by the runtime the list is empty.
131 fn preset_names() -> Vec<PresetId>;
132 }
133}