referrerpolicy=no-referrer-when-downgrade

polkadot_parachain/chain_spec/
mod.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 cumulus_primitives_core::ParaId;
18use polkadot_omni_node_lib::chain_spec::{GenericChainSpec, LoadSpec};
19use sc_chain_spec::{ChainSpec, ChainType};
20use yet_another_parachain::yet_another_parachain_config;
21
22pub mod asset_hubs;
23pub mod bridge_hubs;
24pub mod collectives;
25pub mod coretime;
26pub mod glutton;
27pub mod penpal;
28pub mod people;
29pub mod yet_another_parachain;
30
31/// Extracts the normalized chain id and parachain id from the input chain id.
32/// (H/T to Phala for the idea)
33/// E.g. "penpal-kusama-2004" yields ("penpal-kusama", Some(2004))
34fn extract_parachain_id<'a>(
35	id: &'a str,
36	para_prefixes: &[&str],
37) -> (&'a str, &'a str, Option<ParaId>) {
38	for para_prefix in para_prefixes {
39		if let Some(suffix) = id.strip_prefix(para_prefix) {
40			let para_id: u32 = suffix.parse().expect("Invalid parachain-id suffix");
41			return (&id[..para_prefix.len() - 1], id, Some(para_id.into()));
42		}
43	}
44
45	(id, id, None)
46}
47
48#[derive(Debug)]
49pub(crate) struct ChainSpecLoader;
50
51impl LoadSpec for ChainSpecLoader {
52	fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, String> {
53		Ok(match id {
54			// - Default-like
55			"staging" => Box::new(penpal::staging_penpal_local_config()),
56			"tick" => Box::new(GenericChainSpec::from_json_bytes(
57				&include_bytes!("../../chain-specs/tick.json")[..],
58			)?),
59			"trick" => Box::new(GenericChainSpec::from_json_bytes(
60				&include_bytes!("../../chain-specs/trick.json")[..],
61			)?),
62			"track" => Box::new(GenericChainSpec::from_json_bytes(
63				&include_bytes!("../../chain-specs/track.json")[..],
64			)?),
65
66			// -- Asset Hub Polkadot
67			"asset-hub-polkadot" | "statemint" => Box::new(GenericChainSpec::from_json_bytes(
68				&include_bytes!("../../chain-specs/asset-hub-polkadot.json")[..],
69			)?),
70
71			// -- Asset Hub Kusama
72			"asset-hub-kusama" | "statemine" => Box::new(GenericChainSpec::from_json_bytes(
73				&include_bytes!("../../chain-specs/asset-hub-kusama.json")[..],
74			)?),
75
76			// -- Asset Hub Rococo
77			"asset-hub-rococo-dev" => Box::new(asset_hubs::asset_hub_rococo_development_config()),
78			"asset-hub-rococo-local" => Box::new(asset_hubs::asset_hub_rococo_local_config()),
79			// the chain spec as used for generating the upgrade genesis values
80			"asset-hub-rococo-genesis" => Box::new(asset_hubs::asset_hub_rococo_genesis_config()),
81			"asset-hub-rococo" => Box::new(GenericChainSpec::from_json_bytes(
82				&include_bytes!("../../chain-specs/asset-hub-rococo.json")[..],
83			)?),
84
85			// -- Asset Hub Westend
86			"asset-hub-westend-dev" | "westmint-dev" => {
87				Box::new(asset_hubs::asset_hub_westend_development_config())
88			},
89			"asset-hub-westend-local" | "westmint-local" => {
90				Box::new(asset_hubs::asset_hub_westend_local_config())
91			},
92			// the chain spec as used for generating the upgrade genesis values
93			"asset-hub-westend-genesis" | "westmint-genesis" => {
94				Box::new(asset_hubs::asset_hub_westend_config())
95			},
96			// the shell-based chain spec as used for syncing
97			"asset-hub-westend" | "westmint" => Box::new(GenericChainSpec::from_json_bytes(
98				&include_bytes!("../../chain-specs/asset-hub-westend.json")[..],
99			)?),
100
101			// -- Polkadot Collectives
102			"collectives-polkadot" => Box::new(GenericChainSpec::from_json_bytes(
103				&include_bytes!("../../chain-specs/collectives-polkadot.json")[..],
104			)?),
105
106			// -- Westend Collectives
107			"collectives-westend-dev" => {
108				Box::new(collectives::collectives_westend_development_config())
109			},
110			"collectives-westend-local" => {
111				Box::new(collectives::collectives_westend_local_config())
112			},
113			"collectives-westend" => Box::new(GenericChainSpec::from_json_bytes(
114				&include_bytes!("../../chain-specs/collectives-westend.json")[..],
115			)?),
116
117			// -- BridgeHub
118			bridge_like_id
119				if bridge_like_id.starts_with(bridge_hubs::BridgeHubRuntimeType::ID_PREFIX) =>
120			{
121				bridge_like_id
122					.parse::<bridge_hubs::BridgeHubRuntimeType>()
123					.expect("invalid value")
124					.load_config()?
125			},
126
127			// -- Coretime
128			coretime_like_id
129				if coretime_like_id.starts_with(coretime::CoretimeRuntimeType::ID_PREFIX) =>
130			{
131				coretime_like_id
132					.parse::<coretime::CoretimeRuntimeType>()
133					.expect("invalid value")
134					.load_config()?
135			},
136
137			// -- Penpal
138			id if id.starts_with("penpal-rococo") => {
139				let (_, _, para_id) = extract_parachain_id(&id, &["penpal-rococo-"]);
140				Box::new(penpal::get_penpal_chain_spec(
141					para_id.expect("Must specify parachain id"),
142					"rococo-local",
143				))
144			},
145			id if id.starts_with("penpal-westend") => {
146				let (_, _, para_id) = extract_parachain_id(&id, &["penpal-westend-"]);
147				Box::new(penpal::get_penpal_chain_spec(
148					para_id.expect("Must specify parachain id"),
149					"westend-local",
150				))
151			},
152
153			// -- Glutton Westend
154			id if id.starts_with("glutton-westend-dev") => {
155				let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-dev-"]);
156				Box::new(glutton::glutton_westend_config(
157					para_id.expect("Must specify parachain id"),
158					ChainType::Development,
159					"westend-dev",
160				))
161			},
162			id if id.starts_with("glutton-westend-local") => {
163				let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-local-"]);
164				Box::new(glutton::glutton_westend_config(
165					para_id.expect("Must specify parachain id"),
166					ChainType::Local,
167					"westend-local",
168				))
169			},
170			// the chain spec as used for generating the upgrade genesis values
171			id if id.starts_with("glutton-westend-genesis") => {
172				let (_, _, para_id) = extract_parachain_id(&id, &["glutton-westend-genesis-"]);
173				Box::new(glutton::glutton_westend_config(
174					para_id.expect("Must specify parachain id"),
175					ChainType::Live,
176					"westend",
177				))
178			},
179
180			id if id.starts_with("yap-") => {
181				let tok: Vec<String> = id.split('-').map(|s| s.to_owned()).collect();
182				assert!(
183					tok.len() == 4,
184					"Invalid YAP chain id, should be 'yap-<relay>-<chaintype>-<para-id>'"
185				);
186				let relay = if &tok[2] == "live" { tok[1].clone() } else { tok[1..=2].join("-") };
187				let chain_type = match tok[2].as_str() {
188					"local" => ChainType::Local,
189					"dev" => ChainType::Development,
190					"live" => ChainType::Live,
191					_ => unimplemented!("Unknown chain type {}", tok[2]),
192				};
193				let para_id: u32 =
194					tok[3].parse().expect(&format!("Illegal para id '{}' provided", tok[3]));
195
196				Box::new(yet_another_parachain_config(relay, chain_type, para_id))
197			},
198
199			// -- People
200			people_like_id if people_like_id.starts_with(people::PeopleRuntimeType::ID_PREFIX) => {
201				people_like_id
202					.parse::<people::PeopleRuntimeType>()
203					.expect("invalid value")
204					.load_config()?
205			},
206
207			// -- Fallback (generic chainspec)
208			"" => {
209				log::warn!(
210					"No ChainSpec.id specified, so using default one, based on Penpal runtime"
211				);
212				Box::new(penpal::staging_penpal_local_config())
213			},
214
215			// -- Loading a specific spec from disk
216			path => Box::new(GenericChainSpec::from_json_file(path.into())?),
217		})
218	}
219}