git.delta.rocks / unique-network / refs/commits / d53bf3cc5d4a

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.38pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;3940pub enum RuntimeId {41	Unique,42	Quartz,43	Opal,44	Unknown(String),45}4647pub trait RuntimeIdentification {48	fn runtime_id(&self) -> RuntimeId;49}5051impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {52	fn runtime_id(&self) -> RuntimeId {53		#[cfg(feature = "unique-runtime")]54		if self.id().starts_with("unique") {55			return RuntimeId::Unique;56		}5758		#[cfg(feature = "quartz-runtime")]59		if self.id().starts_with("quartz") {60			return RuntimeId::Quartz;61		}6263		if self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet" {64			return RuntimeId::Opal;65		}6667		RuntimeId::Unknown(self.id().into())68	}69}7071/// Helper function to generate a crypto pair from seed72pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {73	TPublic::Pair::from_string(&format!("//{}", seed), None)74		.expect("static values are valid; qed")75		.public()76}7778/// The extensions for the [`ChainSpec`].79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]80#[serde(deny_unknown_fields)]81pub struct Extensions {82	/// The relay chain of the Parachain.83	pub relay_chain: String,84	/// The id of the Parachain.85	pub para_id: u32,86}8788impl Extensions {89	/// Try to get the extension from the given `ChainSpec`.90	pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {91		sc_chain_spec::get_extension(chain_spec.extensions())92	}93}9495type AccountPublic = <Signature as Verify>::Signer;9697/// Helper function to generate an account ID from seed98pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId99where100	AccountPublic: From<<TPublic::Pair as Pair>::Public>,101{102	AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()103}104105pub fn development_config() -> OpalChainSpec {106	let mut properties = Map::new();107	properties.insert("tokenSymbol".into(), "OPL".into());108	properties.insert("tokenDecimals".into(), 15.into());109	properties.insert("ss58Format".into(), 42.into());110111	OpalChainSpec::from_genesis(112		// Name113		"Development",114		// ID115		"dev",116		ChainType::Local,117		move || {118			testnet_genesis(119				// Sudo account120				get_account_id_from_seed::<sr25519::Public>("Alice"),121				vec![122					get_from_seed::<AuraId>("Alice"),123					get_from_seed::<AuraId>("Bob"),124				],125				// Pre-funded accounts126				vec![127					get_account_id_from_seed::<sr25519::Public>("Alice"),128					get_account_id_from_seed::<sr25519::Public>("Bob"),129				],130				1000.into(),131			)132		},133		// Bootnodes134		vec![],135		// Telemetry136		None,137		// Protocol ID138		None,139		None,140		// Properties141		Some(properties),142		// Extensions143		Extensions {144			relay_chain: "rococo-dev".into(),145			para_id: 1000,146		},147	)148}149150pub fn local_testnet_rococo_config() -> OpalChainSpec {151	OpalChainSpec::from_genesis(152		// Name153		"Local Testnet",154		// ID155		"local_testnet",156		ChainType::Local,157		move || {158			testnet_genesis(159				// Sudo account160				get_account_id_from_seed::<sr25519::Public>("Alice"),161				vec![162					get_from_seed::<AuraId>("Alice"),163					get_from_seed::<AuraId>("Bob"),164				],165				// Pre-funded accounts166				vec![167					get_account_id_from_seed::<sr25519::Public>("Alice"),168					get_account_id_from_seed::<sr25519::Public>("Bob"),169					get_account_id_from_seed::<sr25519::Public>("Charlie"),170					get_account_id_from_seed::<sr25519::Public>("Dave"),171					get_account_id_from_seed::<sr25519::Public>("Eve"),172					get_account_id_from_seed::<sr25519::Public>("Ferdie"),173					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),174					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),175					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),176					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),177					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),178					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),179				],180				1000.into(),181			)182		},183		// Bootnodes184		vec![],185		// Telemetry186		None,187		// Protocol ID188		None,189		None,190		// Properties191		None,192		// Extensions193		Extensions {194			relay_chain: "rococo-local".into(),195			para_id: 1000,196		},197	)198}199200pub fn local_testnet_westend_config() -> OpalChainSpec {201	OpalChainSpec::from_genesis(202		// Name203		"Local Testnet",204		// ID205		"local_testnet",206		ChainType::Local,207		move || {208			testnet_genesis(209				// Sudo account210				get_account_id_from_seed::<sr25519::Public>("Alice"),211				vec![212					get_from_seed::<AuraId>("Alice"),213					get_from_seed::<AuraId>("Bob"),214					get_from_seed::<AuraId>("Charlie"),215					get_from_seed::<AuraId>("Dave"),216					get_from_seed::<AuraId>("Eve"),217				],218				// Pre-funded accounts219				vec![220					get_account_id_from_seed::<sr25519::Public>("Alice"),221					get_account_id_from_seed::<sr25519::Public>("Bob"),222					get_account_id_from_seed::<sr25519::Public>("Charlie"),223					get_account_id_from_seed::<sr25519::Public>("Dave"),224					get_account_id_from_seed::<sr25519::Public>("Eve"),225					get_account_id_from_seed::<sr25519::Public>("Ferdie"),226					get_account_id_from_seed::<sr25519::Public>("Alice//stash"),227					get_account_id_from_seed::<sr25519::Public>("Bob//stash"),228					get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),229					get_account_id_from_seed::<sr25519::Public>("Dave//stash"),230					get_account_id_from_seed::<sr25519::Public>("Eve//stash"),231					get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),232				],233				1000.into(),234			)235		},236		// Bootnodes237		vec![],238		// Telemetry239		None,240		// Protocol ID241		None,242		None,243		// Properties244		None,245		// Extensions246		Extensions {247			relay_chain: "westend-local".into(),248			para_id: 1000,249		},250	)251}252253fn testnet_genesis(254	root_key: AccountId,255	initial_authorities: Vec<AuraId>,256	endowed_accounts: Vec<AccountId>,257	id: ParaId,258) -> opal_runtime::GenesisConfig {259	use opal_runtime::*;260261	GenesisConfig {262		system: SystemConfig {263			code: WASM_BINARY264				.expect("WASM binary was not build, please build it!")265				.to_vec(),266		},267		balances: BalancesConfig {268			balances: endowed_accounts269				.iter()270				.cloned()271				// 1e13 UNQ272				.map(|k| (k, 1 << 100))273				.collect(),274		},275		treasury: Default::default(),276		sudo: SudoConfig {277			key: Some(root_key),278		},279		vesting: VestingConfig { vesting: vec![] },280		parachain_info: ParachainInfoConfig { parachain_id: id },281		parachain_system: Default::default(),282		aura: AuraConfig {283			authorities: initial_authorities,284		},285		aura_ext: Default::default(),286		evm: EVMConfig {287			accounts: BTreeMap::new(),288		},289		ethereum: EthereumConfig {},290	}291}