referrerpolicy=no-referrer-when-downgrade

frame_benchmarking_cli/shared/
genesis_state.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
18use crate::overhead::command::ParachainExtension;
19use sc_chain_spec::{ChainSpec, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
20use sc_cli::Result;
21use serde_json::Value;
22use sp_storage::{well_known_keys::CODE, Storage};
23use sp_wasm_interface::HostFunctions;
24use std::{borrow::Cow, path::PathBuf};
25
26/// When the runtime could not build the genesis storage.
27const ERROR_CANNOT_BUILD_GENESIS: &str = "The runtime returned \
28an error when trying to build the genesis storage. Please ensure that all pallets \
29define a genesis config that can be built. This can be tested with: \
30https://github.com/paritytech/polkadot-sdk/pull/3412";
31
32/// Warn when using the chain spec to generate the genesis state.
33pub const WARN_SPEC_GENESIS_CTOR: &'static str = "Using the chain spec instead of the runtime to \
34generate the genesis state is deprecated. Please remove the `--chain`/`--dev`/`--local` argument, \
35point `--runtime` to your runtime blob and set `--genesis-builder=runtime`. This warning may \
36become a hard error any time after December 2024.";
37
38/// Defines how the chain specification shall be used to build the genesis storage.
39pub enum SpecGenesisSource {
40	/// Use preset provided by the runtime embedded in the chain specification.
41	Runtime(String),
42	/// Use provided chain-specification JSON file.
43	SpecJson,
44	/// Use default storage.
45	None,
46}
47
48/// Defines how the genesis storage shall be built.
49pub enum GenesisStateHandler {
50	ChainSpec(Box<dyn ChainSpec>, SpecGenesisSource),
51	Runtime(Vec<u8>, Option<String>),
52}
53
54impl GenesisStateHandler {
55	/// Populate the genesis storage.
56	///
57	/// If the raw storage is derived from a named genesis preset, `json_patcher` is can be used to
58	/// inject values into the preset.
59	pub fn build_storage<HF: HostFunctions>(
60		&self,
61		json_patcher: Option<Box<dyn FnOnce(Value) -> Value + 'static>>,
62	) -> Result<Storage> {
63		match self {
64			GenesisStateHandler::ChainSpec(chain_spec, source) => match source {
65				SpecGenesisSource::Runtime(preset) => {
66					let mut storage = chain_spec.build_storage()?;
67					let code_bytes = storage
68						.top
69						.remove(CODE)
70						.ok_or("chain spec genesis does not contain code")?;
71					genesis_from_code::<HF>(code_bytes.as_slice(), preset, json_patcher)
72				},
73				SpecGenesisSource::SpecJson => chain_spec
74					.build_storage()
75					.map_err(|e| format!("{ERROR_CANNOT_BUILD_GENESIS}\nError: {e}").into()),
76				SpecGenesisSource::None => Ok(Storage::default()),
77			},
78			GenesisStateHandler::Runtime(code_bytes, Some(preset)) =>
79				genesis_from_code::<HF>(code_bytes.as_slice(), preset, json_patcher),
80			GenesisStateHandler::Runtime(_, None) => Ok(Storage::default()),
81		}
82	}
83
84	/// Get the runtime code blob.
85	pub fn get_code_bytes(&self) -> Result<Cow<[u8]>> {
86		match self {
87			GenesisStateHandler::ChainSpec(chain_spec, _) => {
88				let mut storage = chain_spec.build_storage()?;
89				storage
90					.top
91					.remove(CODE)
92					.map(|code| Cow::from(code))
93					.ok_or("chain spec genesis does not contain code".into())
94			},
95			GenesisStateHandler::Runtime(code_bytes, _) => Ok(code_bytes.into()),
96		}
97	}
98}
99
100pub fn chain_spec_from_path<HF: HostFunctions>(
101	chain: PathBuf,
102) -> Result<(Box<dyn ChainSpec>, Option<u32>)> {
103	let spec = GenericChainSpec::<ParachainExtension, HF>::from_json_file(chain)
104		.map_err(|e| format!("Unable to load chain spec: {:?}", e))?;
105
106	let para_id_from_chain_spec = spec.extensions().para_id;
107	Ok((Box::new(spec), para_id_from_chain_spec))
108}
109
110fn genesis_from_code<EHF: HostFunctions>(
111	code: &[u8],
112	genesis_builder_preset: &String,
113	storage_patcher: Option<Box<dyn FnOnce(Value) -> Value>>,
114) -> Result<Storage> {
115	let genesis_config_caller = GenesisConfigBuilderRuntimeCaller::<(
116		sp_io::SubstrateHostFunctions,
117		frame_benchmarking::benchmarking::HostFunctions,
118		EHF,
119	)>::new(code);
120
121	let mut preset_json = genesis_config_caller.get_named_preset(Some(genesis_builder_preset))?;
122	if let Some(patcher) = storage_patcher {
123		preset_json = patcher(preset_json);
124	}
125
126	let mut storage =
127		genesis_config_caller.get_storage_for_patch(preset_json).inspect_err(|e| {
128			let presets = genesis_config_caller.preset_names().unwrap_or_default();
129			log::error!(
130				"Please pick one of the available presets with \
131        `--genesis-builder-preset=<PRESET>`. Available presets ({}): {:?}. Error: {:?}",
132				presets.len(),
133				presets,
134				e
135			);
136		})?;
137
138	storage.top.insert(CODE.into(), code.into());
139
140	Ok(storage)
141}