referrerpolicy=no-referrer-when-downgrade

polkadot_parachain/chain_spec/
coretime.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use polkadot_omni_node_lib::chain_spec::GenericChainSpec;
18use sc_chain_spec::{ChainSpec, ChainType};
19use std::{borrow::Cow, str::FromStr};
20
21/// Collects all supported Coretime configurations.
22#[derive(Debug, PartialEq, Clone, Copy)]
23pub enum CoretimeRuntimeType {
24	Kusama,
25	KusamaLocal,
26
27	Polkadot,
28	PolkadotLocal,
29
30	// Live
31	Rococo,
32	// Local
33	RococoLocal,
34	// Benchmarks
35	RococoDevelopment,
36
37	// Live
38	Westend,
39	// Local
40	WestendLocal,
41	// Benchmarks
42	WestendDevelopment,
43}
44
45impl FromStr for CoretimeRuntimeType {
46	type Err = String;
47
48	fn from_str(value: &str) -> Result<Self, Self::Err> {
49		match value {
50			kusama::CORETIME_KUSAMA => Ok(CoretimeRuntimeType::Kusama),
51			kusama::CORETIME_KUSAMA_LOCAL => Ok(CoretimeRuntimeType::KusamaLocal),
52			polkadot::CORETIME_POLKADOT => Ok(CoretimeRuntimeType::Polkadot),
53			polkadot::CORETIME_POLKADOT_LOCAL => Ok(CoretimeRuntimeType::PolkadotLocal),
54			rococo::CORETIME_ROCOCO => Ok(CoretimeRuntimeType::Rococo),
55			rococo::CORETIME_ROCOCO_LOCAL => Ok(CoretimeRuntimeType::RococoLocal),
56			rococo::CORETIME_ROCOCO_DEVELOPMENT => Ok(CoretimeRuntimeType::RococoDevelopment),
57			westend::CORETIME_WESTEND => Ok(CoretimeRuntimeType::Westend),
58			westend::CORETIME_WESTEND_LOCAL => Ok(CoretimeRuntimeType::WestendLocal),
59			westend::CORETIME_WESTEND_DEVELOPMENT => Ok(CoretimeRuntimeType::WestendDevelopment),
60			_ => Err(format!("Value '{}' is not configured yet", value)),
61		}
62	}
63}
64
65impl From<CoretimeRuntimeType> for &str {
66	fn from(runtime_type: CoretimeRuntimeType) -> Self {
67		match runtime_type {
68			CoretimeRuntimeType::Kusama => kusama::CORETIME_KUSAMA,
69			CoretimeRuntimeType::KusamaLocal => kusama::CORETIME_KUSAMA_LOCAL,
70			CoretimeRuntimeType::Polkadot => polkadot::CORETIME_POLKADOT,
71			CoretimeRuntimeType::PolkadotLocal => polkadot::CORETIME_POLKADOT_LOCAL,
72			CoretimeRuntimeType::Rococo => rococo::CORETIME_ROCOCO,
73			CoretimeRuntimeType::RococoLocal => rococo::CORETIME_ROCOCO_LOCAL,
74			CoretimeRuntimeType::RococoDevelopment => rococo::CORETIME_ROCOCO_DEVELOPMENT,
75			CoretimeRuntimeType::Westend => westend::CORETIME_WESTEND,
76			CoretimeRuntimeType::WestendLocal => westend::CORETIME_WESTEND_LOCAL,
77			CoretimeRuntimeType::WestendDevelopment => westend::CORETIME_WESTEND_DEVELOPMENT,
78		}
79	}
80}
81
82impl From<CoretimeRuntimeType> for ChainType {
83	fn from(runtime_type: CoretimeRuntimeType) -> Self {
84		match runtime_type {
85			CoretimeRuntimeType::Kusama |
86			CoretimeRuntimeType::Polkadot |
87			CoretimeRuntimeType::Rococo |
88			CoretimeRuntimeType::Westend => ChainType::Live,
89			CoretimeRuntimeType::KusamaLocal |
90			CoretimeRuntimeType::PolkadotLocal |
91			CoretimeRuntimeType::RococoLocal |
92			CoretimeRuntimeType::WestendLocal => ChainType::Local,
93			CoretimeRuntimeType::RococoDevelopment | CoretimeRuntimeType::WestendDevelopment =>
94				ChainType::Development,
95		}
96	}
97}
98
99impl CoretimeRuntimeType {
100	pub const ID_PREFIX: &'static str = "coretime";
101
102	pub fn load_config(&self) -> Result<Box<dyn ChainSpec>, String> {
103		match self {
104			CoretimeRuntimeType::Kusama => Ok(Box::new(GenericChainSpec::from_json_bytes(
105				&include_bytes!("../../chain-specs/coretime-kusama.json")[..],
106			)?)),
107			CoretimeRuntimeType::Polkadot => Ok(Box::new(GenericChainSpec::from_json_bytes(
108				&include_bytes!("../../chain-specs/coretime-polkadot.json")[..],
109			)?)),
110			CoretimeRuntimeType::Rococo => Ok(Box::new(GenericChainSpec::from_json_bytes(
111				&include_bytes!("../../chain-specs/coretime-rococo.json")[..],
112			)?)),
113			CoretimeRuntimeType::RococoLocal =>
114				Ok(Box::new(rococo::local_config(*self, "rococo-local"))),
115			CoretimeRuntimeType::RococoDevelopment =>
116				Ok(Box::new(rococo::local_config(*self, "rococo-dev"))),
117			CoretimeRuntimeType::Westend => Ok(Box::new(GenericChainSpec::from_json_bytes(
118				&include_bytes!("../../../parachains/chain-specs/coretime-westend.json")[..],
119			)?)),
120			CoretimeRuntimeType::WestendLocal =>
121				Ok(Box::new(westend::local_config(*self, "westend-local"))),
122			CoretimeRuntimeType::WestendDevelopment =>
123				Ok(Box::new(westend::local_config(*self, "westend-dev"))),
124			other => Err(std::format!(
125				"No default config present for {:?}, you should provide a chain-spec as json file!",
126				other
127			)),
128		}
129	}
130}
131
132/// Generate the name directly from the ChainType
133pub fn chain_type_name(chain_type: &ChainType) -> Cow<str> {
134	match chain_type {
135		ChainType::Development => "Development",
136		ChainType::Local => "Local",
137		ChainType::Live => "Live",
138		ChainType::Custom(name) => name,
139	}
140	.into()
141}
142
143/// Sub-module for Rococo setup.
144pub mod rococo {
145	use super::{chain_type_name, CoretimeRuntimeType};
146	use polkadot_omni_node_lib::chain_spec::{Extensions, GenericChainSpec};
147	use sc_chain_spec::ChainType;
148
149	pub(crate) const CORETIME_ROCOCO: &str = "coretime-rococo";
150	pub(crate) const CORETIME_ROCOCO_LOCAL: &str = "coretime-rococo-local";
151	pub(crate) const CORETIME_ROCOCO_DEVELOPMENT: &str = "coretime-rococo-dev";
152
153	pub fn local_config(runtime_type: CoretimeRuntimeType, relay_chain: &str) -> GenericChainSpec {
154		// Rococo defaults
155		let mut properties = sc_chain_spec::Properties::new();
156		properties.insert("ss58Format".into(), 42.into());
157		properties.insert("tokenSymbol".into(), "ROC".into());
158		properties.insert("tokenDecimals".into(), 12.into());
159
160		let chain_type = runtime_type.into();
161		let chain_name = format!("Coretime Rococo {}", chain_type_name(&chain_type));
162
163		let wasm_binary = if matches!(chain_type, ChainType::Local | ChainType::Development) {
164			coretime_rococo_runtime::fast_runtime_binary::WASM_BINARY
165				.expect("WASM binary was not built, please build it!")
166		} else {
167			coretime_rococo_runtime::WASM_BINARY
168				.expect("WASM binary was not built, please build it!")
169		};
170
171		GenericChainSpec::builder(
172			wasm_binary,
173			Extensions::new_with_relay_chain(relay_chain.to_string()),
174		)
175		.with_name(&chain_name)
176		.with_id(runtime_type.into())
177		.with_chain_type(chain_type.clone())
178		.with_genesis_config_preset_name(match chain_type {
179			ChainType::Development => sp_genesis_builder::DEV_RUNTIME_PRESET,
180			ChainType::Local => sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET,
181			_ => panic!("chain_type: {chain_type:?} not supported here!"),
182		})
183		.with_properties(properties)
184		.build()
185	}
186}
187
188/// Sub-module for Westend setup.
189pub mod westend {
190	use super::{chain_type_name, CoretimeRuntimeType, GenericChainSpec};
191	use polkadot_omni_node_lib::chain_spec::Extensions;
192	use sc_chain_spec::ChainType;
193
194	pub(crate) const CORETIME_WESTEND: &str = "coretime-westend";
195	pub(crate) const CORETIME_WESTEND_LOCAL: &str = "coretime-westend-local";
196	pub(crate) const CORETIME_WESTEND_DEVELOPMENT: &str = "coretime-westend-dev";
197
198	pub fn local_config(runtime_type: CoretimeRuntimeType, relay_chain: &str) -> GenericChainSpec {
199		// westend defaults
200		let mut properties = sc_chain_spec::Properties::new();
201		properties.insert("ss58Format".into(), 42.into());
202		properties.insert("tokenSymbol".into(), "WND".into());
203		properties.insert("tokenDecimals".into(), 12.into());
204
205		let chain_type = runtime_type.into();
206		let chain_name = format!("Coretime Westend {}", chain_type_name(&chain_type));
207
208		GenericChainSpec::builder(
209			coretime_westend_runtime::WASM_BINARY
210				.expect("WASM binary was not built, please build it!"),
211			Extensions::new_with_relay_chain(relay_chain.to_string()),
212		)
213		.with_name(&chain_name)
214		.with_id(runtime_type.into())
215		.with_chain_type(chain_type.clone())
216		.with_genesis_config_preset_name(match chain_type {
217			ChainType::Development => sp_genesis_builder::DEV_RUNTIME_PRESET,
218			ChainType::Local => sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET,
219			_ => panic!("chain_type: {chain_type:?} not supported here!"),
220		})
221		.with_properties(properties)
222		.build()
223	}
224}
225
226pub mod kusama {
227	pub(crate) const CORETIME_KUSAMA: &str = "coretime-kusama";
228	pub(crate) const CORETIME_KUSAMA_LOCAL: &str = "coretime-kusama-local";
229}
230
231pub mod polkadot {
232	pub(crate) const CORETIME_POLKADOT: &str = "coretime-polkadot";
233	pub(crate) const CORETIME_POLKADOT_LOCAL: &str = "coretime-polkadot-local";
234}