referrerpolicy=no-referrer-when-downgrade

substrate_test_runtime/
genesismap.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//! Tool for creating the genesis block.
19
20use super::{
21	currency, substrate_test_pallet, wasm_binary_unwrap, AccountId, Balance, RuntimeGenesisConfig,
22};
23use codec::Encode;
24use sc_service::construct_genesis_block;
25use sp_core::{
26	sr25519,
27	storage::{well_known_keys, StateVersion, Storage},
28	Pair,
29};
30use sp_keyring::Sr25519Keyring;
31use sp_runtime::{
32	traits::{Block as BlockT, Hash as HashT, Header as HeaderT},
33	BuildStorage,
34};
35
36/// Builder for generating storage from substrate-test-runtime genesis config.
37///
38/// Default storage can be extended with additional key-value pairs.
39pub struct GenesisStorageBuilder {
40	/// Authorities accounts used by any component requiring an authority set (e.g. babe).
41	authorities: Vec<AccountId>,
42	/// Accounts to be endowed with some funds.
43	balances: Vec<(AccountId, u64)>,
44	/// Override default number of heap pages.
45	heap_pages_override: Option<u64>,
46	/// Additional storage key pairs that will be added to the genesis map.
47	extra_storage: Storage,
48	/// Optional wasm code override.
49	wasm_code: Option<Vec<u8>>,
50}
51
52impl Default for GenesisStorageBuilder {
53	/// Creates a builder with default settings for `substrate_test_runtime`.
54	fn default() -> Self {
55		Self::new(
56			vec![
57				Sr25519Keyring::Alice.into(),
58				Sr25519Keyring::Bob.into(),
59				Sr25519Keyring::Charlie.into(),
60			],
61			(0..16_usize)
62				.into_iter()
63				.map(|i| Sr25519Keyring::numeric(i).public())
64				.chain(vec![
65					Sr25519Keyring::Alice.into(),
66					Sr25519Keyring::Bob.into(),
67					Sr25519Keyring::Charlie.into(),
68				])
69				.collect(),
70			1000 * currency::DOLLARS,
71		)
72	}
73}
74
75impl GenesisStorageBuilder {
76	/// Creates a storage builder for genesis config. `substrage test runtime`
77	/// [`RuntimeGenesisConfig`] is initialized with provided `authorities`, `endowed_accounts` with
78	/// given balance. Key-value pairs from `extra_storage` will be injected into built storage.
79	/// `HEAP_PAGES` key and value will also be placed into storage.
80	pub fn new(
81		authorities: Vec<AccountId>,
82		endowed_accounts: Vec<AccountId>,
83		balance: Balance,
84	) -> Self {
85		GenesisStorageBuilder {
86			authorities,
87			balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(),
88			heap_pages_override: None,
89			extra_storage: Default::default(),
90			wasm_code: None,
91		}
92	}
93
94	/// Override default wasm code to be placed into RuntimeGenesisConfig.
95	pub fn with_wasm_code(mut self, wasm_code: &Option<Vec<u8>>) -> Self {
96		self.wasm_code = wasm_code.clone();
97		self
98	}
99
100	pub fn with_heap_pages(mut self, heap_pages_override: Option<u64>) -> Self {
101		self.heap_pages_override = heap_pages_override;
102		self
103	}
104
105	pub fn with_extra_storage(mut self, storage: Storage) -> Self {
106		self.extra_storage = storage;
107		self
108	}
109
110	/// A `RuntimeGenesisConfig` from internal configuration
111	pub fn genesis_config(&self) -> RuntimeGenesisConfig {
112		let authorities_sr25519: Vec<_> = self
113			.authorities
114			.clone()
115			.into_iter()
116			.map(|id| sr25519::Public::from(id))
117			.collect();
118
119		RuntimeGenesisConfig {
120			system: Default::default(),
121			babe: pallet_babe::GenesisConfig {
122				authorities: authorities_sr25519
123					.clone()
124					.into_iter()
125					.map(|x| (x.into(), 1))
126					.collect(),
127				..Default::default()
128			},
129			substrate_test: substrate_test_pallet::GenesisConfig {
130				authorities: authorities_sr25519.clone(),
131				..Default::default()
132			},
133			balances: pallet_balances::GenesisConfig {
134				balances: self.balances.clone(),
135				..Default::default()
136			},
137		}
138	}
139
140	/// Builds the `RuntimeGenesisConfig` and returns its storage.
141	pub fn build(self) -> Storage {
142		let mut storage = self
143			.genesis_config()
144			.build_storage()
145			.expect("Build storage from substrate-test-runtime RuntimeGenesisConfig");
146
147		if let Some(heap_pages) = self.heap_pages_override {
148			storage.top.insert(well_known_keys::HEAP_PAGES.into(), heap_pages.encode());
149		}
150
151		storage.top.insert(
152			well_known_keys::CODE.into(),
153			self.wasm_code.clone().unwrap_or(wasm_binary_unwrap().to_vec()),
154		);
155
156		storage.top.extend(self.extra_storage.top.clone());
157		storage.children_default.extend(self.extra_storage.children_default.clone());
158
159		storage
160	}
161}
162
163pub fn insert_genesis_block(storage: &mut Storage) -> sp_core::hash::H256 {
164	let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
165		let state_root =
166			<<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
167				child_content.data.clone().into_iter().collect(),
168				sp_runtime::StateVersion::V1,
169			);
170		(sk.clone(), state_root.encode())
171	});
172	// add child roots to storage
173	storage.top.extend(child_roots);
174	let state_root = <<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
175		storage.top.clone().into_iter().collect(),
176		sp_runtime::StateVersion::V1,
177	);
178	let block: crate::Block = construct_genesis_block(state_root, StateVersion::V1);
179	let genesis_hash = block.header.hash();
180
181	genesis_hash
182}