git.delta.rocks / unique-network / refs/commits / 2ddc70da9e1d

difftreelog

source

node/src/chain_spec.rs4.7 KiBsourcehistory
1use sp_core::{Pair, Public, sr25519};2use node_template_runtime::{3	AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,4	SudoConfig, SystemConfig, WASM_BINARY, Signature5};6use sp_consensus_aura::sr25519::{AuthorityId as AuraId};7use grandpa_primitives::{AuthorityId as GrandpaId};8use sc_service;9use sp_runtime::traits::{Verify, IdentifyAccount};1011// Note this is the URL for the telemetry server12//const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";1314/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.15pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;1617/// The chain specification option. This is expected to come in from the CLI and18/// is little more than one of a number of alternatives which can easily be converted19/// from a string (`--chain=...`) into a `ChainSpec`.20#[derive(Clone, Debug)]21pub enum Alternative {22	/// Whatever the current runtime is, with just Alice as an auth.23	Development,24	/// Whatever the current runtime is, with simple Alice/Bob auths.25	LocalTestnet,26}2728/// Helper function to generate a crypto pair from seed29pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {30	TPublic::Pair::from_string(&format!("//{}", seed), None)31		.expect("static values are valid; qed")32		.public()33}3435type AccountPublic = <Signature as Verify>::Signer;3637/// Helper function to generate an account ID from seed38pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where39	AccountPublic: From<<TPublic::Pair as Pair>::Public>40{41	AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()42}4344/// Helper function to generate an authority key for Aura45pub fn get_authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {46	(47		get_from_seed::<AuraId>(s),48		get_from_seed::<GrandpaId>(s),49	)50}5152impl Alternative {53	/// Get an actual chain config from one of the alternatives.54	pub(crate) fn load(self) -> Result<ChainSpec, String> {55		Ok(match self {56			Alternative::Development => ChainSpec::from_genesis(57				"Development",58				"dev",59				|| testnet_genesis(60					vec![61						get_authority_keys_from_seed("Alice"),62					],63					get_account_id_from_seed::<sr25519::Public>("Alice"),64					vec![65						get_account_id_from_seed::<sr25519::Public>("Alice"),66						get_account_id_from_seed::<sr25519::Public>("Bob"),67						get_account_id_from_seed::<sr25519::Public>("Alice//stash"),68						get_account_id_from_seed::<sr25519::Public>("Bob//stash"),69					],70					true,71				),72				vec![],73				None,74				None,75				None,76				None77			),78			Alternative::LocalTestnet => ChainSpec::from_genesis(79				"Local Testnet",80				"local_testnet",81				|| testnet_genesis(82					vec![83						get_authority_keys_from_seed("Alice"),84						get_authority_keys_from_seed("Bob"),85					],86					get_account_id_from_seed::<sr25519::Public>("Alice"),87					vec![88						get_account_id_from_seed::<sr25519::Public>("Alice"),89						get_account_id_from_seed::<sr25519::Public>("Bob"),90						get_account_id_from_seed::<sr25519::Public>("Charlie"),91						get_account_id_from_seed::<sr25519::Public>("Dave"),92						get_account_id_from_seed::<sr25519::Public>("Eve"),93						get_account_id_from_seed::<sr25519::Public>("Ferdie"),94						get_account_id_from_seed::<sr25519::Public>("Alice//stash"),95						get_account_id_from_seed::<sr25519::Public>("Bob//stash"),96						get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),97						get_account_id_from_seed::<sr25519::Public>("Dave//stash"),98						get_account_id_from_seed::<sr25519::Public>("Eve//stash"),99						get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),100					],101					true,102				),103				vec![],104				None,105				None,106				None,107				None108			),109		})110	}111112	pub(crate) fn from(s: &str) -> Option<Self> {113		match s {114			"dev" => Some(Alternative::Development),115			"" | "local" => Some(Alternative::LocalTestnet),116			_ => None,117		}118	}119}120121fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,122	root_key: AccountId,123	endowed_accounts: Vec<AccountId>,124	_enable_println: bool) -> GenesisConfig {125	GenesisConfig {126		system: Some(SystemConfig {127			code: WASM_BINARY.to_vec(),128			changes_trie_config: Default::default(),129		}),130		balances: Some(BalancesConfig {131			balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),132		}),133		aura: Some(AuraConfig {134			authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),135		}),136		grandpa: Some(GrandpaConfig {137			authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),138		}),139		sudo: Some(SudoConfig {140			key: root_key,141		}),142	}143}144145pub fn load_spec(id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {146	Ok(match Alternative::from(id) {147		Some(spec) => Box::new(spec.load()?),148		None => Box::new(ChainSpec::from_json_file(std::path::PathBuf::from(id))?),149	})150}