referrerpolicy=no-referrer-when-downgrade

polkadot_parachain/chain_spec/
bridge_hubs.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::str::FromStr;
20
21/// Collects all supported BridgeHub configurations
22#[derive(Debug, PartialEq)]
23pub enum BridgeHubRuntimeType {
24	Kusama,
25	KusamaLocal,
26
27	Polkadot,
28	PolkadotLocal,
29
30	Rococo,
31	RococoLocal,
32	// used by benchmarks
33	RococoDevelopment,
34
35	Westend,
36	WestendLocal,
37	// used by benchmarks
38	WestendDevelopment,
39}
40
41impl FromStr for BridgeHubRuntimeType {
42	type Err = String;
43
44	fn from_str(value: &str) -> Result<Self, Self::Err> {
45		match value {
46			polkadot::BRIDGE_HUB_POLKADOT => Ok(BridgeHubRuntimeType::Polkadot),
47			polkadot::BRIDGE_HUB_POLKADOT_LOCAL => Ok(BridgeHubRuntimeType::PolkadotLocal),
48			kusama::BRIDGE_HUB_KUSAMA => Ok(BridgeHubRuntimeType::Kusama),
49			kusama::BRIDGE_HUB_KUSAMA_LOCAL => Ok(BridgeHubRuntimeType::KusamaLocal),
50			westend::BRIDGE_HUB_WESTEND => Ok(BridgeHubRuntimeType::Westend),
51			westend::BRIDGE_HUB_WESTEND_LOCAL => Ok(BridgeHubRuntimeType::WestendLocal),
52			westend::BRIDGE_HUB_WESTEND_DEVELOPMENT => Ok(BridgeHubRuntimeType::WestendDevelopment),
53			rococo::BRIDGE_HUB_ROCOCO => Ok(BridgeHubRuntimeType::Rococo),
54			rococo::BRIDGE_HUB_ROCOCO_LOCAL => Ok(BridgeHubRuntimeType::RococoLocal),
55			rococo::BRIDGE_HUB_ROCOCO_DEVELOPMENT => Ok(BridgeHubRuntimeType::RococoDevelopment),
56			_ => Err(format!("Value '{}' is not configured yet", value)),
57		}
58	}
59}
60
61impl BridgeHubRuntimeType {
62	pub const ID_PREFIX: &'static str = "bridge-hub";
63
64	pub fn load_config(&self) -> Result<Box<dyn ChainSpec>, String> {
65		match self {
66			BridgeHubRuntimeType::Polkadot => Ok(Box::new(GenericChainSpec::from_json_bytes(
67				&include_bytes!("../../chain-specs/bridge-hub-polkadot.json")[..],
68			)?)),
69			BridgeHubRuntimeType::Kusama => Ok(Box::new(GenericChainSpec::from_json_bytes(
70				&include_bytes!("../../chain-specs/bridge-hub-kusama.json")[..],
71			)?)),
72			BridgeHubRuntimeType::Westend => Ok(Box::new(GenericChainSpec::from_json_bytes(
73				&include_bytes!("../../chain-specs/bridge-hub-westend.json")[..],
74			)?)),
75			BridgeHubRuntimeType::WestendLocal => Ok(Box::new(westend::local_config(
76				westend::BRIDGE_HUB_WESTEND_LOCAL,
77				"Westend BridgeHub Local",
78				"westend-local",
79				ChainType::Local,
80			))),
81			BridgeHubRuntimeType::WestendDevelopment => Ok(Box::new(westend::local_config(
82				westend::BRIDGE_HUB_WESTEND_DEVELOPMENT,
83				"Westend BridgeHub Development",
84				"westend-dev",
85				ChainType::Development,
86			))),
87			BridgeHubRuntimeType::Rococo => Ok(Box::new(GenericChainSpec::from_json_bytes(
88				&include_bytes!("../../chain-specs/bridge-hub-rococo.json")[..],
89			)?)),
90			BridgeHubRuntimeType::RococoLocal => Ok(Box::new(rococo::local_config(
91				rococo::BRIDGE_HUB_ROCOCO_LOCAL,
92				"Rococo BridgeHub Local",
93				"rococo-local",
94				|_| (),
95				ChainType::Local,
96			))),
97			BridgeHubRuntimeType::RococoDevelopment => Ok(Box::new(rococo::local_config(
98				rococo::BRIDGE_HUB_ROCOCO_DEVELOPMENT,
99				"Rococo BridgeHub Development",
100				"rococo-dev",
101				|_| (),
102				ChainType::Development,
103			))),
104			other => Err(std::format!("No default config present for {:?}", other)),
105		}
106	}
107}
108
109/// Check if 'id' satisfy BridgeHub-like format
110fn ensure_id(id: &str) -> Result<&str, String> {
111	if id.starts_with(BridgeHubRuntimeType::ID_PREFIX) {
112		Ok(id)
113	} else {
114		Err(format!(
115			"Invalid 'id' attribute ({}), should start with prefix: {}",
116			id,
117			BridgeHubRuntimeType::ID_PREFIX
118		))
119	}
120}
121
122/// Sub-module for Rococo setup
123pub mod rococo {
124	use super::ChainType;
125	use polkadot_omni_node_lib::chain_spec::{Extensions, GenericChainSpec};
126
127	pub(crate) const BRIDGE_HUB_ROCOCO: &str = "bridge-hub-rococo";
128	pub(crate) const BRIDGE_HUB_ROCOCO_LOCAL: &str = "bridge-hub-rococo-local";
129	pub(crate) const BRIDGE_HUB_ROCOCO_DEVELOPMENT: &str = "bridge-hub-rococo-dev";
130
131	pub fn local_config<ModifyProperties: Fn(&mut sc_chain_spec::Properties)>(
132		id: &str,
133		chain_name: &str,
134		relay_chain: &str,
135		modify_props: ModifyProperties,
136		chain_type: ChainType,
137	) -> GenericChainSpec {
138		// Rococo defaults
139		let mut properties = sc_chain_spec::Properties::new();
140		properties.insert("ss58Format".into(), 42.into());
141		properties.insert("tokenSymbol".into(), "ROC".into());
142		properties.insert("tokenDecimals".into(), 12.into());
143		modify_props(&mut properties);
144
145		GenericChainSpec::builder(
146			bridge_hub_rococo_runtime::WASM_BINARY
147				.expect("WASM binary was not built, please build it!"),
148			Extensions::new_with_relay_chain(relay_chain.to_string()),
149		)
150		.with_name(chain_name)
151		.with_id(super::ensure_id(id).expect("invalid id"))
152		.with_chain_type(chain_type.clone())
153		.with_genesis_config_preset_name(match chain_type {
154			ChainType::Development => sp_genesis_builder::DEV_RUNTIME_PRESET,
155			ChainType::Local => sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET,
156			_ => panic!("chain_type: {chain_type:?} not supported here!"),
157		})
158		.with_properties(properties)
159		.build()
160	}
161}
162
163/// Sub-module for Kusama setup
164pub mod kusama {
165	pub(crate) const BRIDGE_HUB_KUSAMA: &str = "bridge-hub-kusama";
166	pub(crate) const BRIDGE_HUB_KUSAMA_LOCAL: &str = "bridge-hub-kusama-local";
167}
168
169/// Sub-module for Westend setup.
170pub mod westend {
171	use super::ChainType;
172	use polkadot_omni_node_lib::chain_spec::{Extensions, GenericChainSpec};
173
174	pub(crate) const BRIDGE_HUB_WESTEND: &str = "bridge-hub-westend";
175	pub(crate) const BRIDGE_HUB_WESTEND_LOCAL: &str = "bridge-hub-westend-local";
176	pub(crate) const BRIDGE_HUB_WESTEND_DEVELOPMENT: &str = "bridge-hub-westend-dev";
177
178	pub fn local_config(
179		id: &str,
180		chain_name: &str,
181		relay_chain: &str,
182		chain_type: ChainType,
183	) -> GenericChainSpec {
184		let mut properties = sc_chain_spec::Properties::new();
185		properties.insert("tokenSymbol".into(), "WND".into());
186		properties.insert("tokenDecimals".into(), 12.into());
187
188		GenericChainSpec::builder(
189			bridge_hub_westend_runtime::WASM_BINARY
190				.expect("WASM binary was not build, please build it!"),
191			Extensions::new_with_relay_chain(relay_chain.to_string()),
192		)
193		.with_name(chain_name)
194		.with_id(super::ensure_id(id).expect("invalid id"))
195		.with_chain_type(chain_type.clone())
196		.with_genesis_config_preset_name(match chain_type {
197			ChainType::Development => sp_genesis_builder::DEV_RUNTIME_PRESET,
198			ChainType::Local => sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET,
199			_ => panic!("chain_type: {chain_type:?} not supported here!"),
200		})
201		.with_properties(properties)
202		.build()
203	}
204}
205
206/// Sub-module for Polkadot setup
207pub mod polkadot {
208	pub(crate) const BRIDGE_HUB_POLKADOT: &str = "bridge-hub-polkadot";
209	pub(crate) const BRIDGE_HUB_POLKADOT_LOCAL: &str = "bridge-hub-polkadot-local";
210}