git.delta.rocks / unique-network / refs/commits / 5985fa11530c

difftreelog

Fix load_spec and is_opal

Daniel Shiposha2022-03-14parent: #89a64dc.patch.diff
in: master

2 files changed

modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
before · node/cli/src/chain_spec.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use cumulus_primitives_core::ParaId;18use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};19use sc_service::ChainType;20use sp_core::{sr25519, Pair, Public};21use sp_runtime::traits::{IdentifyAccount, Verify};22use std::collections::BTreeMap;2324use serde::{Deserialize, Serialize};25use serde_json::map::Map;2627use unique_runtime_common::types::*;2829/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.30pub type ChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;3132pub trait RuntimeIdentification {33	fn is_unique(&self) -> bool;3435	fn is_quartz(&self) -> bool;3637	fn is_opal(&self) -> bool;38}3940impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {41	fn is_unique(&self) -> bool {42		self.id().starts_with("unique")43	}4445	fn is_quartz(&self) -> bool {46		self.id().starts_with("quartz")47	}4849	fn is_opal(&self) -> bool {50		self.id().starts_with("opal")51	}52}5354/// Helper function to generate a crypto pair from seed55pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {56	TPublic::Pair::from_string(&format!("//{}", seed), None)57		.expect("static values are valid; qed")58		.public()59}6061/// The extensions for the [`ChainSpec`].62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]63#[serde(deny_unknown_fields)]64pub struct Extensions {65	/// The relay chain of the Parachain.66	pub relay_chain: String,67	/// The id of the Parachain.68	pub para_id: u32,69}7071impl Extensions {72	/// Try to get the extension from the given `ChainSpec`.73	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {74		sc_chain_spec::get_extension(chain_spec.extensions())75	}76}7778type AccountPublic = <Signature as Verify>::Signer;7980/// Helper function to generate an account ID from seed81pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId82where83	AccountPublic: From<<TPublic::Pair as Pair>::Public>,84{85	AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()86}8788pub fn development_config() -> ChainSpec {89	let mut properties = Map::new();90	properties.insert("tokenSymbol".into(), "OPL".into());91	properties.insert("tokenDecimals".into(), 15.into());92	properties.insert("ss58Format".into(), 42.into());9394	ChainSpec::from_genesis(95		// Name96		"Development",97		// ID98		"dev",99		ChainType::Local,100		move || {101			testnet_genesis(102				// Sudo account103				get_account_id_from_seed::<sr25519::Public>("Alice"),104				vec![105					get_from_seed::<AuraId>("Alice"),106					get_from_seed::<AuraId>("Bob"),107				],108				// Pre-funded accounts109				vec![110					get_account_id_from_seed::<sr25519::Public>("Alice"),111					get_account_id_from_seed::<sr25519::Public>("Bob"),112				],113				1000.into(),114			)115		},116		// Bootnodes117		vec![],118		// Telemetry119		None,120		// Protocol ID121		None,122		None,123		// Properties124		Some(properties),125		// Extensions126		Extensions {127			relay_chain: "rococo-dev".into(),128			para_id: 1000,129		},130	)131}132133pub fn local_testnet_rococo_config() -> ChainSpec {134	ChainSpec::from_genesis(135		// Name136		"Local Testnet",137		// ID138		"local_testnet",139		ChainType::Local,140		move || {141			testnet_genesis(142				// Sudo account143				get_account_id_from_seed::<sr25519::Public>("Alice"),144				vec![145					get_from_seed::<AuraId>("Alice"),146					get_from_seed::<AuraId>("Bob"),147				],148				// Pre-funded accounts149				vec![150					get_account_id_from_seed::<sr25519::Public>("Alice"),151					get_account_id_from_seed::<sr25519::Public>("Bob"),152					get_account_id_from_seed::<sr25519::Public>("Charlie"),153					get_account_id_from_seed::<sr25519::Public>("Dave"),154					get_account_id_from_seed::<sr25519::Public>("Eve"),155					get_account_id_from_seed::<sr25519::Public>("Ferdie"),156					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),157					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),158					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),159					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),160					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),161					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),162				],163				1000.into(),164			)165		},166		// Bootnodes167		vec![],168		// Telemetry169		None,170		// Protocol ID171		None,172		None,173		// Properties174		None,175		// Extensions176		Extensions {177			relay_chain: "rococo-local".into(),178			para_id: 1000,179		},180	)181}182183pub fn local_testnet_westend_config() -> ChainSpec {184	ChainSpec::from_genesis(185		// Name186		"Local Testnet",187		// ID188		"local_testnet",189		ChainType::Local,190		move || {191			testnet_genesis(192				// Sudo account193				get_account_id_from_seed::<sr25519::Public>("Alice"),194				vec![195					get_from_seed::<AuraId>("Alice"),196					get_from_seed::<AuraId>("Bob"),197					get_from_seed::<AuraId>("Charlie"),198					get_from_seed::<AuraId>("Dave"),199					get_from_seed::<AuraId>("Eve"),200				],201				// Pre-funded accounts202				vec![203					get_account_id_from_seed::<sr25519::Public>("Alice"),204					get_account_id_from_seed::<sr25519::Public>("Bob"),205					get_account_id_from_seed::<sr25519::Public>("Charlie"),206					get_account_id_from_seed::<sr25519::Public>("Dave"),207					get_account_id_from_seed::<sr25519::Public>("Eve"),208					get_account_id_from_seed::<sr25519::Public>("Ferdie"),209					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),210					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),211					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),212					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),213					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),214					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),215				],216				1000.into(),217			)218		},219		// Bootnodes220		vec![],221		// Telemetry222		None,223		// Protocol ID224		None,225		None,226		// Properties227		None,228		// Extensions229		Extensions {230			relay_chain: "westend-local".into(),231			para_id: 1000,232		},233	)234}235236fn testnet_genesis(237	root_key: AccountId,238	initial_authorities: Vec<AuraId>,239	endowed_accounts: Vec<AccountId>,240	id: ParaId,241) -> unique_runtime::GenesisConfig {242	use unique_runtime::*;243244	GenesisConfig {245		system: SystemConfig {246			code: WASM_BINARY247				.expect("WASM binary was not build, please build it!")248				.to_vec(),249		},250		balances: BalancesConfig {251			balances: endowed_accounts252				.iter()253				.cloned()254				// 1e13 UNQ255				.map(|k| (k, 1 << 100))256				.collect(),257		},258		treasury: Default::default(),259		sudo: SudoConfig {260			key: Some(root_key),261		},262		vesting: VestingConfig { vesting: vec![] },263		parachain_info: ParachainInfoConfig { parachain_id: id },264		parachain_system: Default::default(),265		aura: AuraConfig {266			authorities: initial_authorities,267		},268		aura_ext: Default::default(),269		evm: EVMConfig {270			accounts: BTreeMap::new(),271		},272		ethereum: EthereumConfig {},273	}274}
after · node/cli/src/chain_spec.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use cumulus_primitives_core::ParaId;18use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};19use sc_service::ChainType;20use sp_core::{sr25519, Pair, Public};21use sp_runtime::traits::{IdentifyAccount, Verify};22use std::collections::BTreeMap;2324use serde::{Deserialize, Serialize};25use serde_json::map::Map;2627use unique_runtime_common::types::*;2829/// The `ChainSpec` parameterized for the unique runtime.30#[cfg(feature = "unique-runtime")]31pub type UniqueChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;3233/// The `ChainSpec` parameterized for the quartz runtime.34#[cfg(feature = "quartz-runtime")]35pub type QuartzChainSpec = sc_service::GenericChainSpec<quartz_runtime::GenesisConfig, Extensions>;3637/// The `ChainSpec` parameterized for the opal runtime.38#[cfg(feature = "opal-runtime")]39pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;4041pub trait RuntimeIdentification {42	fn is_unique(&self) -> bool;4344	fn is_quartz(&self) -> bool;4546	fn is_opal(&self) -> bool;47}4849impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {50	fn is_unique(&self) -> bool {51		self.id().starts_with("unique")52	}5354	fn is_quartz(&self) -> bool {55		self.id().starts_with("quartz")56	}5758	fn is_opal(&self) -> bool {59		self.id().starts_with("opal")60		|| self.id() == "dev"61		|| self.id() == "local_testnet"62	}63}6465/// Helper function to generate a crypto pair from seed66pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {67	TPublic::Pair::from_string(&format!("//{}", seed), None)68		.expect("static values are valid; qed")69		.public()70}7172/// The extensions for the [`ChainSpec`].73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]74#[serde(deny_unknown_fields)]75pub struct Extensions {76	/// The relay chain of the Parachain.77	pub relay_chain: String,78	/// The id of the Parachain.79	pub para_id: u32,80}8182impl Extensions {83	/// Try to get the extension from the given `ChainSpec`.84	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {85		sc_chain_spec::get_extension(chain_spec.extensions())86	}87}8889type AccountPublic = <Signature as Verify>::Signer;9091/// Helper function to generate an account ID from seed92pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId93where94	AccountPublic: From<<TPublic::Pair as Pair>::Public>,95{96	AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()97}9899pub fn development_config() -> OpalChainSpec {100	let mut properties = Map::new();101	properties.insert("tokenSymbol".into(), "OPL".into());102	properties.insert("tokenDecimals".into(), 15.into());103	properties.insert("ss58Format".into(), 42.into());104105	OpalChainSpec::from_genesis(106		// Name107		"Development",108		// ID109		"dev",110		ChainType::Local,111		move || {112			testnet_genesis(113				// Sudo account114				get_account_id_from_seed::<sr25519::Public>("Alice"),115				vec![116					get_from_seed::<AuraId>("Alice"),117					get_from_seed::<AuraId>("Bob"),118				],119				// Pre-funded accounts120				vec![121					get_account_id_from_seed::<sr25519::Public>("Alice"),122					get_account_id_from_seed::<sr25519::Public>("Bob"),123				],124				1000.into(),125			)126		},127		// Bootnodes128		vec![],129		// Telemetry130		None,131		// Protocol ID132		None,133		None,134		// Properties135		Some(properties),136		// Extensions137		Extensions {138			relay_chain: "rococo-dev".into(),139			para_id: 1000,140		},141	)142}143144pub fn local_testnet_rococo_config() -> OpalChainSpec {145	OpalChainSpec::from_genesis(146		// Name147		"Local Testnet",148		// ID149		"local_testnet",150		ChainType::Local,151		move || {152			testnet_genesis(153				// Sudo account154				get_account_id_from_seed::<sr25519::Public>("Alice"),155				vec![156					get_from_seed::<AuraId>("Alice"),157					get_from_seed::<AuraId>("Bob"),158				],159				// Pre-funded accounts160				vec![161					get_account_id_from_seed::<sr25519::Public>("Alice"),162					get_account_id_from_seed::<sr25519::Public>("Bob"),163					get_account_id_from_seed::<sr25519::Public>("Charlie"),164					get_account_id_from_seed::<sr25519::Public>("Dave"),165					get_account_id_from_seed::<sr25519::Public>("Eve"),166					get_account_id_from_seed::<sr25519::Public>("Ferdie"),167					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),168					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),169					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),170					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),171					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),172					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),173				],174				1000.into(),175			)176		},177		// Bootnodes178		vec![],179		// Telemetry180		None,181		// Protocol ID182		None,183		None,184		// Properties185		None,186		// Extensions187		Extensions {188			relay_chain: "rococo-local".into(),189			para_id: 1000,190		},191	)192}193194pub fn local_testnet_westend_config() -> OpalChainSpec {195	OpalChainSpec::from_genesis(196		// Name197		"Local Testnet",198		// ID199		"local_testnet",200		ChainType::Local,201		move || {202			testnet_genesis(203				// Sudo account204				get_account_id_from_seed::<sr25519::Public>("Alice"),205				vec![206					get_from_seed::<AuraId>("Alice"),207					get_from_seed::<AuraId>("Bob"),208					get_from_seed::<AuraId>("Charlie"),209					get_from_seed::<AuraId>("Dave"),210					get_from_seed::<AuraId>("Eve"),211				],212				// Pre-funded accounts213				vec![214					get_account_id_from_seed::<sr25519::Public>("Alice"),215					get_account_id_from_seed::<sr25519::Public>("Bob"),216					get_account_id_from_seed::<sr25519::Public>("Charlie"),217					get_account_id_from_seed::<sr25519::Public>("Dave"),218					get_account_id_from_seed::<sr25519::Public>("Eve"),219					get_account_id_from_seed::<sr25519::Public>("Ferdie"),220					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),221					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),222					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),223					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),224					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),225					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),226				],227				1000.into(),228			)229		},230		// Bootnodes231		vec![],232		// Telemetry233		None,234		// Protocol ID235		None,236		None,237		// Properties238		None,239		// Extensions240		Extensions {241			relay_chain: "westend-local".into(),242			para_id: 1000,243		},244	)245}246247fn testnet_genesis(248	root_key: AccountId,249	initial_authorities: Vec<AuraId>,250	endowed_accounts: Vec<AccountId>,251	id: ParaId,252) -> opal_runtime::GenesisConfig {253	use opal_runtime::*;254255	GenesisConfig {256		system: SystemConfig {257			code: WASM_BINARY258				.expect("WASM binary was not build, please build it!")259				.to_vec(),260		},261		balances: BalancesConfig {262			balances: endowed_accounts263				.iter()264				.cloned()265				// 1e13 UNQ266				.map(|k| (k, 1 << 100))267				.collect(),268		},269		treasury: Default::default(),270		sudo: SudoConfig {271			key: Some(root_key),272		},273		vesting: VestingConfig { vesting: vec![] },274		parachain_info: ParachainInfoConfig { parachain_id: id },275		parachain_system: Default::default(),276		aura: AuraConfig {277			authorities: initial_authorities,278		},279		aura_ext: Default::default(),280		evm: EVMConfig {281			accounts: BTreeMap::new(),282		},283		ethereum: EthereumConfig {},284	}285}
modifiednode/cli/src/command.rsdiffbeforeafterboth
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -75,15 +75,41 @@
 }
 
 fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
-	Ok(match id {
-		"westend-local" => Box::new(chain_spec::local_testnet_westend_config()),
-		"rococo-local" => Box::new(chain_spec::local_testnet_rococo_config()),
-		"dev" => Box::new(chain_spec::development_config()),
-		"" | "local" => Box::new(chain_spec::local_testnet_rococo_config()),
-		path => Box::new(chain_spec::ChainSpec::from_json_file(
-			std::path::PathBuf::from(path),
-		)?),
-	})
+	match id {
+		"westend-local" => Ok(Box::new(chain_spec::local_testnet_westend_config())),
+		"rococo-local" => Ok(Box::new(chain_spec::local_testnet_rococo_config())),
+		"dev" => Ok(Box::new(chain_spec::development_config())),
+		"" | "local" => Ok(Box::new(chain_spec::local_testnet_rococo_config())),
+		path => {
+			let path = std::path::PathBuf::from(path);
+			let chain_spec = Box::new(
+				chain_spec::UniqueChainSpec::from_json_file(path.clone())?
+			) as Box<dyn sc_service::ChainSpec>;
+
+			#[cfg(feature = "unique-runtime")]
+			if chain_spec.is_unique() {
+				return Ok(chain_spec);
+			}
+
+			#[cfg(feature = "quartz-runtime")]
+			if chain_spec.is_quartz() {
+				let chain_spec = chain_spec::QuartzChainSpec::from_json_file(
+					path
+				)?;
+				return Ok(Box::new(chain_spec));
+			}
+
+			#[cfg(feature = "opal-runtime")]
+			if chain_spec.is_opal() {
+				let chain_spec = chain_spec::OpalChainSpec::from_json_file(
+					path
+				)?;
+				return Ok(Box::new(chain_spec));
+			}
+
+			Err(no_runtime_err!(chain_spec))
+		},
+	}
 }
 
 impl SubstrateCli for Cli {