git.delta.rocks / unique-network / refs/commits / 5cd3d42aeddb

difftreelog

source

src/chain_spec.rs4.1 KiBsourcehistory
1use primitives::{Pair, Public};2use node_template_runtime::{3	AccountId, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig,4	SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY, 5};6use babe_primitives::{AuthorityId as BabeId};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 stash, controller and session key from seed35pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, GrandpaId, BabeId) {36	(37		get_from_seed::<AccountId>(&format!("{}//stash", seed)),38		get_from_seed::<AccountId>(seed),39		get_from_seed::<GrandpaId>(seed),40		get_from_seed::<BabeId>(seed),41	)42}4344impl Alternative {45	/// Get an actual chain config from one of the alternatives.46	pub(crate) fn load(self) -> Result<ChainSpec, String> {47		Ok(match self {48			Alternative::Development => ChainSpec::from_genesis(49				"Development",50				"dev",51				|| testnet_genesis(vec![52					get_authority_keys_from_seed("Alice"),53				],54				get_from_seed::<AccountId>("Alice"),55				vec![56					get_from_seed::<AccountId>("Alice"),57					get_from_seed::<AccountId>("Bob"),58					get_from_seed::<AccountId>("Alice//stash"),59					get_from_seed::<AccountId>("Bob//stash"),60				],61				true),62				vec![],63				None,64				None,65				None,66				None67			),68			Alternative::LocalTestnet => ChainSpec::from_genesis(69				"Local Testnet",70				"local_testnet",71				|| testnet_genesis(vec![72					get_authority_keys_from_seed("Alice"),73					get_authority_keys_from_seed("Bob"),74				], 75				get_from_seed::<AccountId>("Alice"),76				vec![77					get_from_seed::<AccountId>("Alice"),78					get_from_seed::<AccountId>("Bob"),79					get_from_seed::<AccountId>("Charlie"),80					get_from_seed::<AccountId>("Dave"),81					get_from_seed::<AccountId>("Eve"),82					get_from_seed::<AccountId>("Ferdie"),83					get_from_seed::<AccountId>("Alice//stash"),84					get_from_seed::<AccountId>("Bob//stash"),85					get_from_seed::<AccountId>("Charlie//stash"),86					get_from_seed::<AccountId>("Dave//stash"),87					get_from_seed::<AccountId>("Eve//stash"),88					get_from_seed::<AccountId>("Ferdie//stash"),89				],90				true),91				vec![],92				None,93				None,94				None,95				None96			),97		})98	}99100	pub(crate) fn from(s: &str) -> Option<Self> {101		match s {102			"dev" => Some(Alternative::Development),103			"" | "local" => Some(Alternative::LocalTestnet),104			_ => None,105		}106	}107}108109fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, GrandpaId, BabeId)>,110	root_key: AccountId, 111	endowed_accounts: Vec<AccountId>,112	_enable_println: bool) -> GenesisConfig {113	GenesisConfig {114		system: Some(SystemConfig {115			code: WASM_BINARY.to_vec(),116			changes_trie_config: Default::default(),117		}),118		indices: Some(IndicesConfig {119			ids: endowed_accounts.clone(),120		}),121		balances: Some(BalancesConfig {122			balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),123			vesting: vec![],124		}),125		sudo: Some(SudoConfig {126			key: root_key,127		}),128		babe: Some(BabeConfig {129			authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),130		}),131		grandpa: Some(GrandpaConfig {132			authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),133		}),134	}135}