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
//! For FRAME based runtimes, this runtime interface provides means to interact with
23
//! `RuntimeGenesisConfig`. Runtime provides a default `RuntimeGenesisConfig` structure in a form of
24
//! the JSON blob.
25
//!
26
//! For non-FRAME runtimes this interface is intended to build genesis state of the runtime basing
27
//! on some input arbitrary bytes array. This documentation uses term `RuntimeGenesisConfig`, which
28
//! for non-FRAME runtimes may be understood as the runtime-side entity representing initial runtime
29
//! configuration. The representation of the preset is an arbitrary `Vec<u8>` and does not
30
//! necessarily have to represent a JSON blob.
31
//!
32
//! The runtime may provide a number of partial predefined `RuntimeGenesisConfig` configurations in
33
//! the form of patches which shall be applied on top of the default `RuntimeGenesisConfig`. The
34
//! patch is a JSON blob, which essentially comprises the list of key-value pairs that are to be
35
//! customized in the default runtime genesis config. These predefined configurations are referred
36
//! to as presets.
37
//!
38
//! This allows the runtime to provide a number of predefined configs (e.g. for different
39
//! testnets or development) without neccessity to leak the runtime types outside the itself (e.g.
40
//! node or chain-spec related tools).
41
//!
42
//! This Runtime API allows to interact with `RuntimeGenesisConfig`, in particular:
43
//! - provide the list of available preset names,
44
//! - provide a number of named presets of `RuntimeGenesisConfig`,
45
//! - provide a JSON represention of the default `RuntimeGenesisConfig` (by simply serializing the
46
//!   default `RuntimeGenesisConfig` struct into JSON format),
47
//! - deserialize the full `RuntimeGenesisConfig` from given JSON blob and put the resulting
48
//!   `RuntimeGenesisConfig` structure into the state storage creating the initial runtime's state.
49
//!   Allows to build customized genesis. This operation internally calls `GenesisBuild::build`
50
//!   function for all runtime pallets.
51
//!
52
//! Providing externalities with an empty storage and putting `RuntimeGenesisConfig` into storage
53
//! (by calling `build_state`) allows to construct the raw storage of `RuntimeGenesisConfig`
54
//! which is the foundation for genesis block.
55

            
56
extern crate alloc;
57
use alloc::vec::Vec;
58

            
59
/// The result type alias, used in build methods. `Err` contains formatted error message.
60
pub type Result = core::result::Result<(), sp_runtime::RuntimeString>;
61

            
62
/// The type representing preset ID.
63
pub type PresetId = sp_runtime::RuntimeString;
64

            
65
sp_api::decl_runtime_apis! {
66
	/// API to interact with RuntimeGenesisConfig for the runtime
67
	pub trait GenesisBuilder {
68
		/// Build `RuntimeGenesisConfig` from a JSON blob not using any defaults and store it in the
69
		/// storage.
70
		///
71
		/// In the case of a FRAME-based runtime, this function deserializes the full `RuntimeGenesisConfig` from the given JSON blob and
72
		/// puts it into the storage. If the provided JSON blob is incorrect or incomplete or the
73
		/// deserialization fails, an error is returned.
74
		///
75
		/// Please note that provided JSON blob must contain all `RuntimeGenesisConfig` fields, no
76
		/// defaults will be used.
77
		fn build_state(json: Vec<u8>) -> Result;
78

            
79
		/// Returns a JSON blob representation of the built-in `RuntimeGenesisConfig` identified by
80
		/// `id`.
81
		///
82
		/// If `id` is `None` the function returns JSON blob representation of the default
83
		/// `RuntimeGenesisConfig` struct of the runtime. Implementation must provide default
84
		/// `RuntimeGenesisConfig`.
85
		///
86
		/// Otherwise function returns a JSON representation of the built-in, named
87
		/// `RuntimeGenesisConfig` preset identified by `id`, or `None` if such preset does not
88
		/// exists. Returned `Vec<u8>` contains bytes of JSON blob (patch) which comprises a list of
89
		/// (potentially nested) key-value pairs that are intended for customizing the default
90
		/// runtime genesis config. The patch shall be merged (rfc7386) with the JSON representation
91
		/// of the default `RuntimeGenesisConfig` to create a comprehensive genesis config that can
92
		/// be used in `build_state` method.
93
		fn get_preset(id: &Option<PresetId>) -> Option<Vec<u8>>;
94

            
95
		/// Returns a list of identifiers for available builtin `RuntimeGenesisConfig` presets.
96
		///
97
		/// The presets from the list can be queried with [`GenesisBuilder::get_preset`] method. If
98
		/// no named presets are provided by the runtime the list is empty.
99
		fn preset_names() -> Vec<PresetId>;
100
	}
101
}