referrerpolicy=no-referrer-when-downgrade

polkadot_omni_node_lib/common/
chain_spec.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
17//! Chain spec primitives.
18
19pub use sc_chain_spec::ChainSpec;
20use sc_chain_spec::ChainSpecExtension;
21use serde::{Deserialize, Serialize};
22use std::fmt::Debug;
23
24/// Helper trait used for loading/building a chain spec starting from the chain ID.
25pub trait LoadSpec {
26	/// Load/Build a chain spec starting from the chain ID.
27	fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, String>;
28}
29
30/// Default implementation for `LoadSpec` that just reads a chain spec from the disk.
31pub struct DiskChainSpecLoader;
32
33impl LoadSpec for DiskChainSpecLoader {
34	fn load_spec(&self, path: &str) -> Result<Box<dyn ChainSpec>, String> {
35		Ok(Box::new(GenericChainSpec::from_json_file(path.into())?))
36	}
37}
38
39/// Generic extensions for Parachain ChainSpecs used for extracting the extensions from chain specs.
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecExtension)]
41pub struct Extensions {
42	/// The relay chain of the Parachain. It is kept here only for compatibility reasons until
43	/// people migrate to using the new `Extensions` struct and associated logic in the node
44	/// corresponding to pulling the parachain id from the runtime.
45	#[serde(alias = "relayChain", alias = "RelayChain")]
46	relay_chain: String,
47	/// The id of the Parachain.
48	#[serde(alias = "paraId", alias = "ParaId")]
49	para_id: Option<u32>,
50}
51
52impl Extensions {
53	/// Try to get the extension from the given `ChainSpec`.
54	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
55		sc_chain_spec::get_extension(chain_spec.extensions())
56	}
57
58	/// Create the extensions only with the relay_chain.
59	pub fn new_with_relay_chain(relay_chain: String) -> Self {
60		Extensions { relay_chain, para_id: None }
61	}
62
63	/// Initialize extensions based on given parameters.
64	pub fn new(relay_chain: String, para_id: u32) -> Self {
65		Extensions { relay_chain, para_id: Some(para_id) }
66	}
67
68	/// Para id field getter
69	pub fn para_id(&self) -> Option<u32> {
70		self.para_id
71	}
72
73	/// Relay chain field getter
74	pub fn relay_chain(&self) -> String {
75		self.relay_chain.clone()
76	}
77}
78
79/// Generic chain spec for all polkadot-parachain runtimes
80pub type GenericChainSpec = sc_service::GenericChainSpec<Extensions>;
81
82#[cfg(test)]
83mod tests {
84	use super::*;
85
86	#[test]
87	fn can_decode_extension_camel_and_snake_case() {
88		let camel_case = r#"{"relayChain":"relay","paraId":1}"#;
89		let snake_case = r#"{"relay_chain":"relay","para_id":1}"#;
90		let pascal_case = r#"{"RelayChain":"relay","ParaId":1}"#;
91		let para_id_missing = r#"{"RelayChain":"westend"}"#;
92
93		let camel_case_extension: Extensions = serde_json::from_str(camel_case).unwrap();
94		let snake_case_extension: Extensions = serde_json::from_str(snake_case).unwrap();
95		let pascal_case_extension: Extensions = serde_json::from_str(pascal_case).unwrap();
96		let missing_paraid_extension: Extensions = serde_json::from_str(para_id_missing).unwrap();
97		assert_eq!(camel_case_extension, snake_case_extension);
98		assert_eq!(snake_case_extension, pascal_case_extension);
99		assert_eq!(missing_paraid_extension.relay_chain, "westend".to_string());
100		assert!(missing_paraid_extension.para_id.is_none());
101	}
102}