git.delta.rocks / unique-network / refs/commits / 8f77dc2402c4

difftreelog

source

node/cli/src/chain_spec.rs7.9 KiBsourcehistory
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 enum RuntimeId {42	Unique,43	Quartz,44	Opal,45	Unknown(String),46}4748pub trait RuntimeIdentification {49	fn runtime_id(&self) -> RuntimeId;50}5152impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {53	fn runtime_id(&self) -> RuntimeId {54		#[cfg(feature = "unique-runtime")]55		if self.id().starts_with("unique") {56			return RuntimeId::Unique;57		}5859		#[cfg(feature = "quartz-runtime")]60		if self.id().starts_with("quartz") {61			return RuntimeId::Quartz;62		}6364		#[cfg(feature = "opal-runtime")]65		if self.id().starts_with("opal") {66			return RuntimeId::Opal;67		}6869		RuntimeId::Unknown(self.id().into())70	}71}7273/// Helper function to generate a crypto pair from seed74pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {75	TPublic::Pair::from_string(&format!("//{}", seed), None)76		.expect("static values are valid; qed")77		.public()78}7980/// The extensions for the [`ChainSpec`].81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]82#[serde(deny_unknown_fields)]83pub struct Extensions {84	/// The relay chain of the Parachain.85	pub relay_chain: String,86	/// The id of the Parachain.87	pub para_id: u32,88}8990impl Extensions {91	/// Try to get the extension from the given `ChainSpec`.92	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {93		sc_chain_spec::get_extension(chain_spec.extensions())94	}95}9697type AccountPublic = <Signature as Verify>::Signer;9899/// Helper function to generate an account ID from seed100pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId101where102	AccountPublic: From<<TPublic::Pair as Pair>::Public>,103{104	AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()105}106107pub fn development_config() -> OpalChainSpec {108	let mut properties = Map::new();109	properties.insert("tokenSymbol".into(), "OPL".into());110	properties.insert("tokenDecimals".into(), 15.into());111	properties.insert("ss58Format".into(), 42.into());112113	OpalChainSpec::from_genesis(114		// Name115		"Development",116		// ID117		"dev",118		ChainType::Local,119		move || {120			testnet_genesis(121				// Sudo account122				get_account_id_from_seed::<sr25519::Public>("Alice"),123				vec![124					get_from_seed::<AuraId>("Alice"),125					get_from_seed::<AuraId>("Bob"),126				],127				// Pre-funded accounts128				vec![129					get_account_id_from_seed::<sr25519::Public>("Alice"),130					get_account_id_from_seed::<sr25519::Public>("Bob"),131				],132				1000.into(),133			)134		},135		// Bootnodes136		vec![],137		// Telemetry138		None,139		// Protocol ID140		None,141		None,142		// Properties143		Some(properties),144		// Extensions145		Extensions {146			relay_chain: "rococo-dev".into(),147			para_id: 1000,148		},149	)150}151152pub fn local_testnet_rococo_config() -> OpalChainSpec {153	OpalChainSpec::from_genesis(154		// Name155		"Local Testnet",156		// ID157		"local_testnet",158		ChainType::Local,159		move || {160			testnet_genesis(161				// Sudo account162				get_account_id_from_seed::<sr25519::Public>("Alice"),163				vec![164					get_from_seed::<AuraId>("Alice"),165					get_from_seed::<AuraId>("Bob"),166				],167				// Pre-funded accounts168				vec![169					get_account_id_from_seed::<sr25519::Public>("Alice"),170					get_account_id_from_seed::<sr25519::Public>("Bob"),171					get_account_id_from_seed::<sr25519::Public>("Charlie"),172					get_account_id_from_seed::<sr25519::Public>("Dave"),173					get_account_id_from_seed::<sr25519::Public>("Eve"),174					get_account_id_from_seed::<sr25519::Public>("Ferdie"),175					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),176					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),177					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),178					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),179					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),180					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),181				],182				1000.into(),183			)184		},185		// Bootnodes186		vec![],187		// Telemetry188		None,189		// Protocol ID190		None,191		None,192		// Properties193		None,194		// Extensions195		Extensions {196			relay_chain: "rococo-local".into(),197			para_id: 1000,198		},199	)200}201202pub fn local_testnet_westend_config() -> OpalChainSpec {203	OpalChainSpec::from_genesis(204		// Name205		"Local Testnet",206		// ID207		"local_testnet",208		ChainType::Local,209		move || {210			testnet_genesis(211				// Sudo account212				get_account_id_from_seed::<sr25519::Public>("Alice"),213				vec![214					get_from_seed::<AuraId>("Alice"),215					get_from_seed::<AuraId>("Bob"),216					get_from_seed::<AuraId>("Charlie"),217					get_from_seed::<AuraId>("Dave"),218					get_from_seed::<AuraId>("Eve"),219				],220				// Pre-funded accounts221				vec![222					get_account_id_from_seed::<sr25519::Public>("Alice"),223					get_account_id_from_seed::<sr25519::Public>("Bob"),224					get_account_id_from_seed::<sr25519::Public>("Charlie"),225					get_account_id_from_seed::<sr25519::Public>("Dave"),226					get_account_id_from_seed::<sr25519::Public>("Eve"),227					get_account_id_from_seed::<sr25519::Public>("Ferdie"),228					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),229					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),230					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),231					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),232					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),233					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),234				],235				1000.into(),236			)237		},238		// Bootnodes239		vec![],240		// Telemetry241		None,242		// Protocol ID243		None,244		None,245		// Properties246		None,247		// Extensions248		Extensions {249			relay_chain: "westend-local".into(),250			para_id: 1000,251		},252	)253}254255fn testnet_genesis(256	root_key: AccountId,257	initial_authorities: Vec<AuraId>,258	endowed_accounts: Vec<AccountId>,259	id: ParaId,260) -> opal_runtime::GenesisConfig {261	use opal_runtime::*;262263	GenesisConfig {264		system: SystemConfig {265			code: WASM_BINARY266				.expect("WASM binary was not build, please build it!")267				.to_vec(),268		},269		balances: BalancesConfig {270			balances: endowed_accounts271				.iter()272				.cloned()273				// 1e13 UNQ274				.map(|k| (k, 1 << 100))275				.collect(),276		},277		treasury: Default::default(),278		sudo: SudoConfig {279			key: Some(root_key),280		},281		vesting: VestingConfig { vesting: vec![] },282		parachain_info: ParachainInfoConfig { parachain_id: id },283		parachain_system: Default::default(),284		aura: AuraConfig {285			authorities: initial_authorities,286		},287		aura_ext: Default::default(),288		evm: EVMConfig {289			accounts: BTreeMap::new(),290		},291		ethereum: EthereumConfig {},292	}293}