difftreelog
feat(chain_spec) depend on runtime feature
in: master
1 file changed
node/cli/src/chain_spec.rsdiffbeforeafterboth1// 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 sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};18use sc_service::ChainType;19use sp_core::{sr25519, Pair, Public};20use sp_runtime::traits::{IdentifyAccount, Verify};21use std::collections::BTreeMap;2223use serde::{Deserialize, Serialize};24use serde_json::map::Map;2526use up_common::types::opaque::*;2728#[cfg(feature = "unique-runtime")]29pub use unique_runtime as default_runtime;3031#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]32pub use quartz_runtime as default_runtime;3334#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]35pub use opal_runtime as default_runtime;3637/// The `ChainSpec` parameterized for the unique runtime.38#[cfg(feature = "unique-runtime")]39pub type UniqueChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;4041/// The `ChainSpec` parameterized for the quartz runtime.42#[cfg(feature = "quartz-runtime")]43pub type QuartzChainSpec = sc_service::GenericChainSpec<quartz_runtime::GenesisConfig, Extensions>;4445/// The `ChainSpec` parameterized for the opal runtime.46pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;4748#[cfg(feature = "unique-runtime")]49pub type DefaultChainSpec = UniqueChainSpec;5051#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]52pub type DefaultChainSpec = QuartzChainSpec;5354#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]55pub type DefaultChainSpec = OpalChainSpec;5657pub enum RuntimeId {58 #[cfg(feature = "unique-runtime")]59 Unique,6061 #[cfg(feature = "quartz-runtime")]62 Quartz,6364 Opal,65 Unknown(String),66}6768pub trait RuntimeIdentification {69 fn runtime_id(&self) -> RuntimeId;70}7172impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {73 fn runtime_id(&self) -> RuntimeId {74 #[cfg(feature = "unique-runtime")]75 if self.id().starts_with("unique") || self.id().starts_with("unq") {76 return RuntimeId::Unique;77 }7879 #[cfg(feature = "quartz-runtime")]80 if self.id().starts_with("quartz") || self.id().starts_with("qtz") {81 return RuntimeId::Quartz;82 }8384 if self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet" {85 return RuntimeId::Opal;86 }8788 RuntimeId::Unknown(self.id().into())89 }90}9192pub enum ServiceId {93 Prod,94 Dev,95}9697pub trait ServiceIdentification {98 fn service_id(&self) -> ServiceId;99}100101impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {102 fn service_id(&self) -> ServiceId {103 if self.id().ends_with("dev") {104 ServiceId::Dev105 } else {106 ServiceId::Prod107 }108 }109}110111/// Helper function to generate a crypto pair from seed112pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {113 TPublic::Pair::from_string(&format!("//{}", seed), None)114 .expect("static values are valid; qed")115 .public()116}117118/// The extensions for the [`ChainSpec`].119#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]120#[serde(deny_unknown_fields)]121pub struct Extensions {122 /// The relay chain of the Parachain.123 pub relay_chain: String,124 /// The id of the Parachain.125 pub para_id: u32,126}127128impl Extensions {129 /// Try to get the extension from the given `ChainSpec`.130 pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {131 sc_chain_spec::get_extension(chain_spec.extensions())132 }133}134135type AccountPublic = <Signature as Verify>::Signer;136137/// Helper function to generate an account ID from seed138pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId139where140 AccountPublic: From<<TPublic::Pair as Pair>::Public>,141{142 AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()143}144145macro_rules! testnet_genesis {146 (147 $runtime:path,148 $root_key:expr,149 $initial_authorities:expr,150 $endowed_accounts:expr,151 $id:expr152 ) => {{153 use $runtime::*;154155 GenesisConfig {156 system: SystemConfig {157 code: WASM_BINARY158 .expect("WASM binary was not build, please build it!")159 .to_vec(),160 },161 balances: BalancesConfig {162 balances: $endowed_accounts163 .iter()164 .cloned()165 // 1e13 UNQ166 .map(|k| (k, 1 << 100))167 .collect(),168 },169 treasury: Default::default(),170 tokens: TokensConfig { balances: vec![] },171 sudo: SudoConfig {172 key: Some($root_key),173 },174 vesting: VestingConfig { vesting: vec![] },175 parachain_info: ParachainInfoConfig {176 parachain_id: $id.into(),177 },178 parachain_system: Default::default(),179 aura: AuraConfig {180 authorities: $initial_authorities,181 },182 aura_ext: Default::default(),183 evm: EVMConfig {184 accounts: BTreeMap::new(),185 },186 ethereum: EthereumConfig {},187 }188 }};189}190191pub fn development_config() -> DefaultChainSpec {192 let mut properties = Map::new();193 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());194 properties.insert("tokenDecimals".into(), 18.into());195 properties.insert(196 "ss58Format".into(),197 default_runtime::SS58Prefix::get().into(),198 );199200 DefaultChainSpec::from_genesis(201 // Name202 format!(203 "{}{}",204 default_runtime::RUNTIME_NAME.to_uppercase(),205 if cfg!(feature = "unique-runtime") {206 ""207 } else {208 " by UNIQUE"209 }210 )211 .as_str(),212 // ID213 format!("{}_dev", default_runtime::RUNTIME_NAME).as_str(),214 ChainType::Local,215 move || {216 testnet_genesis!(217 default_runtime,218 // Sudo account219 get_account_id_from_seed::<sr25519::Public>("Alice"),220 vec![221 get_from_seed::<AuraId>("Alice"),222 get_from_seed::<AuraId>("Bob"),223 ],224 // Pre-funded accounts225 vec![226 get_account_id_from_seed::<sr25519::Public>("Alice"),227 get_account_id_from_seed::<sr25519::Public>("Bob"),228 get_account_id_from_seed::<sr25519::Public>("Charlie"),229 get_account_id_from_seed::<sr25519::Public>("Dave"),230 get_account_id_from_seed::<sr25519::Public>("Eve"),231 get_account_id_from_seed::<sr25519::Public>("Ferdie"),232 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),233 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),234 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),235 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),236 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),237 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),238 ],239 2037240 )241 },242 // Bootnodes243 vec![],244 // Telemetry245 None,246 // Protocol ID247 None,248 None,249 // Properties250 Some(properties),251 // Extensions252 Extensions {253 relay_chain: "rococo-dev".into(),254 para_id: 2037,255 },256 )257}258259pub fn local_testnet_config() -> DefaultChainSpec {260 let mut properties = Map::new();261 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());262 properties.insert("tokenDecimals".into(), 18.into());263 properties.insert(264 "ss58Format".into(),265 default_runtime::SS58Prefix::get().into(),266 );267268 DefaultChainSpec::from_genesis(269 // Name270 format!(271 "{}{}",272 default_runtime::RUNTIME_NAME.to_uppercase(),273 if cfg!(feature = "unique-runtime") {274 ""275 } else {276 " by UNIQUE"277 }278 )279 .as_str(),280 // ID281 format!("{}_local", default_runtime::RUNTIME_NAME).as_str(),282 ChainType::Local,283 move || {284 testnet_genesis!(285 default_runtime,286 // Sudo account287 get_account_id_from_seed::<sr25519::Public>("Alice"),288 vec![289 get_from_seed::<AuraId>("Alice"),290 get_from_seed::<AuraId>("Bob"),291 ],292 // Pre-funded accounts293 vec![294 get_account_id_from_seed::<sr25519::Public>("Alice"),295 get_account_id_from_seed::<sr25519::Public>("Bob"),296 get_account_id_from_seed::<sr25519::Public>("Charlie"),297 get_account_id_from_seed::<sr25519::Public>("Dave"),298 get_account_id_from_seed::<sr25519::Public>("Eve"),299 get_account_id_from_seed::<sr25519::Public>("Ferdie"),300 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),301 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),302 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),303 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),304 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),305 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),306 ],307 2037308 )309 },310 // Bootnodes311 vec![],312 // Telemetry313 None,314 // Protocol ID315 None,316 None,317 // Properties318 Some(properties),319 // Extensions320 Extensions {321 relay_chain: "westend-local".into(),322 para_id: 2037,323 },324 )325}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 sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};18use sc_service::ChainType;19use sp_core::{sr25519, Pair, Public};20use sp_runtime::traits::{IdentifyAccount, Verify};21use std::collections::BTreeMap;2223use serde::{Deserialize, Serialize};24use serde_json::map::Map;2526use up_common::types::opaque::*;2728#[cfg(feature = "unique-runtime")]29pub use unique_runtime as default_runtime;3031#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]32pub use quartz_runtime as default_runtime;3334#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]35pub use opal_runtime as default_runtime;3637/// The `ChainSpec` parameterized for the unique runtime.38#[cfg(feature = "unique-runtime")]39pub type UniqueChainSpec = sc_service::GenericChainSpec<unique_runtime::GenesisConfig, Extensions>;4041/// The `ChainSpec` parameterized for the quartz runtime.42#[cfg(feature = "quartz-runtime")]43pub type QuartzChainSpec = sc_service::GenericChainSpec<quartz_runtime::GenesisConfig, Extensions>;4445/// The `ChainSpec` parameterized for the opal runtime.46pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;4748#[cfg(feature = "unique-runtime")]49pub type DefaultChainSpec = UniqueChainSpec;5051#[cfg(all(not(feature = "unique-runtime"), feature = "quartz-runtime"))]52pub type DefaultChainSpec = QuartzChainSpec;5354#[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))]55pub type DefaultChainSpec = OpalChainSpec;5657pub enum RuntimeId {58 #[cfg(feature = "unique-runtime")]59 Unique,6061 #[cfg(feature = "quartz-runtime")]62 Quartz,6364 Opal,65 Unknown(String),66}676869#[cfg(not(feature = "unique-runtime"))]70/// PARA_ID for Opal/Quartz71const PARA_ID: u32 = 2095;7273#[cfg(feature = "unique-runtime")]74/// PARA_ID for Unique75const PARA_ID: u32 = 2037;7677pub trait RuntimeIdentification {78 fn runtime_id(&self) -> RuntimeId;79}8081impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {82 fn runtime_id(&self) -> RuntimeId {83 #[cfg(feature = "unique-runtime")]84 if self.id().starts_with("unique") || self.id().starts_with("unq") {85 return RuntimeId::Unique;86 }8788 #[cfg(feature = "quartz-runtime")]89 if self.id().starts_with("quartz") || self.id().starts_with("qtz") {90 return RuntimeId::Quartz;91 }9293 if self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet" {94 return RuntimeId::Opal;95 }9697 RuntimeId::Unknown(self.id().into())98 }99}100101pub enum ServiceId {102 Prod,103 Dev,104}105106pub trait ServiceIdentification {107 fn service_id(&self) -> ServiceId;108}109110impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {111 fn service_id(&self) -> ServiceId {112 if self.id().ends_with("dev") {113 ServiceId::Dev114 } else {115 ServiceId::Prod116 }117 }118}119120/// Helper function to generate a crypto pair from seed121pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {122 TPublic::Pair::from_string(&format!("//{}", seed), None)123 .expect("static values are valid; qed")124 .public()125}126127/// The extensions for the [`ChainSpec`].128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]129#[serde(deny_unknown_fields)]130pub struct Extensions {131 /// The relay chain of the Parachain.132 pub relay_chain: String,133 /// The id of the Parachain.134 pub para_id: u32,135}136137impl Extensions {138 /// Try to get the extension from the given `ChainSpec`.139 pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {140 sc_chain_spec::get_extension(chain_spec.extensions())141 }142}143144type AccountPublic = <Signature as Verify>::Signer;145146/// Helper function to generate an account ID from seed147pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId148where149 AccountPublic: From<<TPublic::Pair as Pair>::Public>,150{151 AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()152}153154macro_rules! testnet_genesis {155 (156 $runtime:path,157 $root_key:expr,158 $initial_authorities:expr,159 $endowed_accounts:expr,160 $id:expr161 ) => {{162 use $runtime::*;163164 GenesisConfig {165 system: SystemConfig {166 code: WASM_BINARY167 .expect("WASM binary was not build, please build it!")168 .to_vec(),169 },170 balances: BalancesConfig {171 balances: $endowed_accounts172 .iter()173 .cloned()174 // 1e13 UNQ175 .map(|k| (k, 1 << 100))176 .collect(),177 },178 treasury: Default::default(),179 tokens: TokensConfig { balances: vec![] },180 sudo: SudoConfig {181 key: Some($root_key),182 },183 vesting: VestingConfig { vesting: vec![] },184 parachain_info: ParachainInfoConfig {185 parachain_id: $id.into(),186 },187 parachain_system: Default::default(),188 aura: AuraConfig {189 authorities: $initial_authorities,190 },191 aura_ext: Default::default(),192 evm: EVMConfig {193 accounts: BTreeMap::new(),194 },195 ethereum: EthereumConfig {},196 }197 }};198}199200pub fn development_config() -> DefaultChainSpec {201 let mut properties = Map::new();202 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());203 properties.insert("tokenDecimals".into(), 18.into());204 properties.insert(205 "ss58Format".into(),206 default_runtime::SS58Prefix::get().into(),207 );208209 DefaultChainSpec::from_genesis(210 // Name211 format!(212 "{}{}",213 default_runtime::RUNTIME_NAME.to_uppercase(),214 if cfg!(feature = "unique-runtime") {215 ""216 } else {217 " by UNIQUE"218 }219 )220 .as_str(),221 // ID222 format!("{}_dev", default_runtime::RUNTIME_NAME).as_str(),223 ChainType::Local,224 move || {225 testnet_genesis!(226 default_runtime,227 // Sudo account228 get_account_id_from_seed::<sr25519::Public>("Alice"),229 vec![230 get_from_seed::<AuraId>("Alice"),231 get_from_seed::<AuraId>("Bob"),232 ],233 // Pre-funded accounts234 vec![235 get_account_id_from_seed::<sr25519::Public>("Alice"),236 get_account_id_from_seed::<sr25519::Public>("Bob"),237 get_account_id_from_seed::<sr25519::Public>("Charlie"),238 get_account_id_from_seed::<sr25519::Public>("Dave"),239 get_account_id_from_seed::<sr25519::Public>("Eve"),240 get_account_id_from_seed::<sr25519::Public>("Ferdie"),241 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),242 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),243 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),244 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),245 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),246 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),247 ],248 PARA_ID249 )250 },251 // Bootnodes252 vec![],253 // Telemetry254 None,255 // Protocol ID256 None,257 None,258 // Properties259 Some(properties),260 // Extensions261 Extensions {262 relay_chain: "rococo-dev".into(),263 para_id: PARA_ID,264 },265 )266}267268pub fn local_testnet_config() -> DefaultChainSpec {269 let mut properties = Map::new();270 properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into());271 properties.insert("tokenDecimals".into(), 18.into());272 properties.insert(273 "ss58Format".into(),274 default_runtime::SS58Prefix::get().into(),275 );276277 DefaultChainSpec::from_genesis(278 // Name279 format!(280 "{}{}",281 default_runtime::RUNTIME_NAME.to_uppercase(),282 if cfg!(feature = "unique-runtime") {283 ""284 } else {285 " by UNIQUE"286 }287 )288 .as_str(),289 // ID290 format!("{}_local", default_runtime::RUNTIME_NAME).as_str(),291 ChainType::Local,292 move || {293 testnet_genesis!(294 default_runtime,295 // Sudo account296 get_account_id_from_seed::<sr25519::Public>("Alice"),297 vec![298 get_from_seed::<AuraId>("Alice"),299 get_from_seed::<AuraId>("Bob"),300 ],301 // Pre-funded accounts302 vec![303 get_account_id_from_seed::<sr25519::Public>("Alice"),304 get_account_id_from_seed::<sr25519::Public>("Bob"),305 get_account_id_from_seed::<sr25519::Public>("Charlie"),306 get_account_id_from_seed::<sr25519::Public>("Dave"),307 get_account_id_from_seed::<sr25519::Public>("Eve"),308 get_account_id_from_seed::<sr25519::Public>("Ferdie"),309 get_account_id_from_seed::<sr25519::Public>("Alice//stash"),310 get_account_id_from_seed::<sr25519::Public>("Bob//stash"),311 get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),312 get_account_id_from_seed::<sr25519::Public>("Dave//stash"),313 get_account_id_from_seed::<sr25519::Public>("Eve//stash"),314 get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),315 ],316 PARA_ID317 )318 },319 // Bootnodes320 vec![],321 // Telemetry322 None,323 // Protocol ID324 None,325 None,326 // Properties327 Some(properties),328 // Extensions329 Extensions {330 relay_chain: "westend-local".into(),331 para_id: PARA_ID,332 },333 )334}