git.delta.rocks / unique-network / refs/commits / 20d08c295b0d

difftreelog

source

src/chain_spec.rs3.9 KiBsourcehistory
1use primitives::{Pair, Public};2use node_template_runtime::{3	AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,4	SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY, 5};6use aura_primitives::sr25519::{AuthorityId as AuraId};7use grandpa_primitives::{AuthorityId as GrandpaId};8use substrate_service;910// Note this is the URL for the telemetry server11//const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";1213/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.14pub type ChainSpec = substrate_service::ChainSpec<GenesisConfig>;1516/// The chain specification option. This is expected to come in from the CLI and17/// is little more than one of a number of alternatives which can easily be converted18/// from a string (`--chain=...`) into a `ChainSpec`.19#[derive(Clone, Debug)]20pub enum Alternative {21	/// Whatever the current runtime is, with just Alice as an auth.22	Development,23	/// Whatever the current runtime is, with simple Alice/Bob auths.24	LocalTestnet,25}2627/// Helper function to generate a crypto pair from seed28pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {29	TPublic::Pair::from_string(&format!("//{}", seed), None)30		.expect("static values are valid; qed")31		.public()32}3334/// Helper function to generate an authority key for Aura35pub fn get_authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) { 36	(37		get_from_seed::<AuraId>(s),38		get_from_seed::<GrandpaId>(s),39	)40}4142impl Alternative {43	/// Get an actual chain config from one of the alternatives.44	pub(crate) fn load(self) -> Result<ChainSpec, String> {45		Ok(match self {46			Alternative::Development => ChainSpec::from_genesis(47				"Development",48				"dev",49				|| testnet_genesis(vec![50					get_authority_keys_from_seed("Alice"),51				],52				get_from_seed::<AccountId>("Alice"),53				vec![54					get_from_seed::<AccountId>("Alice"),55					get_from_seed::<AccountId>("Bob"),56					get_from_seed::<AccountId>("Alice//stash"),57					get_from_seed::<AccountId>("Bob//stash"),58				],59				true),60				vec![],61				None,62				None,63				None,64				None65			),66			Alternative::LocalTestnet => ChainSpec::from_genesis(67				"Local Testnet",68				"local_testnet",69				|| testnet_genesis(vec![70					get_authority_keys_from_seed("Alice"),71					get_authority_keys_from_seed("Bob"),72				], 73				get_from_seed::<AccountId>("Alice"),74				vec![75					get_from_seed::<AccountId>("Alice"),76					get_from_seed::<AccountId>("Bob"),77					get_from_seed::<AccountId>("Charlie"),78					get_from_seed::<AccountId>("Dave"),79					get_from_seed::<AccountId>("Eve"),80					get_from_seed::<AccountId>("Ferdie"),81					get_from_seed::<AccountId>("Alice//stash"),82					get_from_seed::<AccountId>("Bob//stash"),83					get_from_seed::<AccountId>("Charlie//stash"),84					get_from_seed::<AccountId>("Dave//stash"),85					get_from_seed::<AccountId>("Eve//stash"),86					get_from_seed::<AccountId>("Ferdie//stash"),87				],88				true),89				vec![],90				None,91				None,92				None,93				None94			),95		})96	}9798	pub(crate) fn from(s: &str) -> Option<Self> {99		match s {100			"dev" => Some(Alternative::Development),101			"" | "local" => Some(Alternative::LocalTestnet),102			_ => None,103		}104	}105}106107fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,108	root_key: AccountId, 109	endowed_accounts: Vec<AccountId>,110	_enable_println: bool) -> GenesisConfig {111	GenesisConfig {112		system: Some(SystemConfig {113			code: WASM_BINARY.to_vec(),114			changes_trie_config: Default::default(),115		}),116		indices: Some(IndicesConfig {117			ids: endowed_accounts.clone(),118		}),119		balances: Some(BalancesConfig {120			balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),121			vesting: vec![],122		}),123		sudo: Some(SudoConfig {124			key: root_key,125		}),126		aura: Some(AuraConfig {127			authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),128		}),129		grandpa: Some(GrandpaConfig {130			authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),131		}),132	}133}