|
| 1 | +use sp_core::{Pair, Public, sr25519}; |
| 2 | +use datdot_runtime::{ |
| 3 | + primitives::AccountId, primitives::Signature, primitives::Balance, |
| 4 | + BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig, ElectionsConfig, |
| 5 | + SudoConfig, SystemConfig, WASM_BINARY, IndicesConfig, ImOnlineConfig, |
| 6 | + AuthorityDiscoveryConfig, CouncilConfig, TechnicalCommitteeConfig, |
| 7 | + DemocracyConfig, SessionConfig, SessionKeys, |
| 8 | + constants::currency::* |
| 9 | +}; |
| 10 | +use sp_consensus_babe::AuthorityId as BabeId; |
| 11 | +use sp_finality_grandpa::AuthorityId as GrandpaId; |
| 12 | +use pallet_im_online::sr25519::{AuthorityId as ImOnlineId}; |
| 13 | +use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; |
| 14 | +use sp_runtime::traits::{Verify, IdentifyAccount}; |
| 15 | +use sc_service::ChainType; |
| 16 | + |
| 17 | +// Note this is the URL for the telemetry server |
| 18 | +//const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/"; |
| 19 | + |
| 20 | +/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type. |
| 21 | +pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>; |
| 22 | + |
| 23 | +/// Helper function to generate a crypto pair from seed |
| 24 | +pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public { |
| 25 | + TPublic::Pair::from_string(&format!("//{}", seed), None) |
| 26 | + .expect("static values are valid; qed") |
| 27 | + .public() |
| 28 | +} |
| 29 | + |
| 30 | +type AccountPublic = <Signature as Verify>::Signer; |
| 31 | + |
| 32 | +/// Helper function to generate an account ID from seed |
| 33 | +pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where |
| 34 | + AccountPublic: From<<TPublic::Pair as Pair>::Public> |
| 35 | +{ |
| 36 | + AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account() |
| 37 | +} |
| 38 | + |
| 39 | +/// Helper function to generate stash, controller and session key from seed |
| 40 | +pub fn authority_keys_from_seed(seed: &str) -> ( |
| 41 | + AccountId, |
| 42 | + AccountId, |
| 43 | + GrandpaId, |
| 44 | + BabeId, |
| 45 | + ImOnlineId, |
| 46 | + AuthorityDiscoveryId, |
| 47 | +) { |
| 48 | + ( |
| 49 | + get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)), |
| 50 | + get_account_id_from_seed::<sr25519::Public>(seed), |
| 51 | + get_from_seed::<GrandpaId>(seed), |
| 52 | + get_from_seed::<BabeId>(seed), |
| 53 | + get_from_seed::<ImOnlineId>(seed), |
| 54 | + get_from_seed::<AuthorityDiscoveryId>(seed), |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +pub fn development_config() -> ChainSpec { |
| 59 | + ChainSpec::from_genesis( |
| 60 | + "Development", |
| 61 | + "dev", |
| 62 | + ChainType::Development, |
| 63 | + || testnet_genesis( |
| 64 | + vec![ |
| 65 | + authority_keys_from_seed("Alice"), |
| 66 | + ], |
| 67 | + get_account_id_from_seed::<sr25519::Public>("Alice"), |
| 68 | + Some(vec![ |
| 69 | + get_account_id_from_seed::<sr25519::Public>("Alice"), |
| 70 | + get_account_id_from_seed::<sr25519::Public>("Bob"), |
| 71 | + get_account_id_from_seed::<sr25519::Public>("Alice//stash"), |
| 72 | + get_account_id_from_seed::<sr25519::Public>("Bob//stash"), |
| 73 | + ]), |
| 74 | + true, |
| 75 | + ), |
| 76 | + vec![], |
| 77 | + None, |
| 78 | + None, |
| 79 | + None, |
| 80 | + None, |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +pub fn local_testnet_config() -> ChainSpec { |
| 85 | + ChainSpec::from_genesis( |
| 86 | + "Local Testnet", |
| 87 | + "local_testnet", |
| 88 | + ChainType::Local, |
| 89 | + || testnet_genesis( |
| 90 | + vec![ |
| 91 | + authority_keys_from_seed("Alice"), |
| 92 | + authority_keys_from_seed("Bob"), |
| 93 | + ], |
| 94 | + get_account_id_from_seed::<sr25519::Public>("Alice"), |
| 95 | + Some(vec![ |
| 96 | + get_account_id_from_seed::<sr25519::Public>("Alice"), |
| 97 | + get_account_id_from_seed::<sr25519::Public>("Bob"), |
| 98 | + get_account_id_from_seed::<sr25519::Public>("Charlie"), |
| 99 | + get_account_id_from_seed::<sr25519::Public>("Dave"), |
| 100 | + get_account_id_from_seed::<sr25519::Public>("Eve"), |
| 101 | + get_account_id_from_seed::<sr25519::Public>("Ferdie"), |
| 102 | + get_account_id_from_seed::<sr25519::Public>("Alice//stash"), |
| 103 | + get_account_id_from_seed::<sr25519::Public>("Bob//stash"), |
| 104 | + get_account_id_from_seed::<sr25519::Public>("Charlie//stash"), |
| 105 | + get_account_id_from_seed::<sr25519::Public>("Dave//stash"), |
| 106 | + get_account_id_from_seed::<sr25519::Public>("Eve//stash"), |
| 107 | + get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"), |
| 108 | + ]), |
| 109 | + true, |
| 110 | + ), |
| 111 | + vec![], |
| 112 | + None, |
| 113 | + None, |
| 114 | + None, |
| 115 | + None, |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +fn session_keys( |
| 120 | + grandpa: GrandpaId, |
| 121 | + babe: BabeId, |
| 122 | + im_online: ImOnlineId, |
| 123 | + authority_discovery: AuthorityDiscoveryId, |
| 124 | +) -> SessionKeys { |
| 125 | + SessionKeys { grandpa, babe, im_online, authority_discovery } |
| 126 | +} |
| 127 | + |
| 128 | +fn testnet_genesis( |
| 129 | + initial_authorities: Vec<( |
| 130 | + AccountId, |
| 131 | + AccountId, |
| 132 | + GrandpaId, |
| 133 | + BabeId, |
| 134 | + ImOnlineId, |
| 135 | + AuthorityDiscoveryId, |
| 136 | + )>, |
| 137 | +root_key: AccountId, |
| 138 | +endowed_accounts: Option<Vec<AccountId>>, |
| 139 | +enable_println: bool, |
| 140 | +) -> GenesisConfig { |
| 141 | +let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| { |
| 142 | + vec![ |
| 143 | + get_account_id_from_seed::<sr25519::Public>("Alice"), |
| 144 | + get_account_id_from_seed::<sr25519::Public>("Bob"), |
| 145 | + get_account_id_from_seed::<sr25519::Public>("Charlie"), |
| 146 | + get_account_id_from_seed::<sr25519::Public>("Dave"), |
| 147 | + get_account_id_from_seed::<sr25519::Public>("Eve"), |
| 148 | + get_account_id_from_seed::<sr25519::Public>("Ferdie"), |
| 149 | + get_account_id_from_seed::<sr25519::Public>("Alice//stash"), |
| 150 | + get_account_id_from_seed::<sr25519::Public>("Bob//stash"), |
| 151 | + get_account_id_from_seed::<sr25519::Public>("Charlie//stash"), |
| 152 | + get_account_id_from_seed::<sr25519::Public>("Dave//stash"), |
| 153 | + get_account_id_from_seed::<sr25519::Public>("Eve//stash"), |
| 154 | + get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"), |
| 155 | + ] |
| 156 | +}); |
| 157 | +let num_endowed_accounts = endowed_accounts.len(); |
| 158 | + |
| 159 | +const ENDOWMENT: Balance = 10_000_000 * DOLLARS; |
| 160 | +const STASH: Balance = 100 * DOLLARS; |
| 161 | + |
| 162 | +GenesisConfig { |
| 163 | + frame_system: Some(SystemConfig { |
| 164 | + code: WASM_BINARY.to_vec(), |
| 165 | + changes_trie_config: Default::default(), |
| 166 | + }), |
| 167 | + pallet_balances: Some(BalancesConfig { |
| 168 | + balances: endowed_accounts.iter().cloned() |
| 169 | + .map(|k| (k, ENDOWMENT)) |
| 170 | + .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) |
| 171 | + .collect(), |
| 172 | + }), |
| 173 | + pallet_indices: Some(IndicesConfig { |
| 174 | + indices: vec![], |
| 175 | + }), |
| 176 | + pallet_session: Some(SessionConfig { |
| 177 | + keys: initial_authorities.iter().map(|x| { |
| 178 | + (x.0.clone(), x.0.clone(), session_keys( |
| 179 | + x.2.clone(), |
| 180 | + x.3.clone(), |
| 181 | + x.4.clone(), |
| 182 | + x.5.clone(), |
| 183 | + )) |
| 184 | + }).collect::<Vec<_>>(), |
| 185 | + }), |
| 186 | + pallet_democracy: Some(DemocracyConfig::default()), |
| 187 | + pallet_elections_phragmen: Some(ElectionsConfig { |
| 188 | + members: endowed_accounts.iter() |
| 189 | + .take((num_endowed_accounts + 1) / 2) |
| 190 | + .cloned() |
| 191 | + .map(|member| (member, STASH)) |
| 192 | + .collect(), |
| 193 | + }), |
| 194 | + pallet_collective_Instance1: Some(CouncilConfig::default()), |
| 195 | + pallet_collective_Instance2: Some(TechnicalCommitteeConfig { |
| 196 | + members: endowed_accounts.iter() |
| 197 | + .take((num_endowed_accounts + 1) / 2) |
| 198 | + .cloned() |
| 199 | + .collect(), |
| 200 | + phantom: Default::default(), |
| 201 | + }), |
| 202 | + pallet_sudo: Some(SudoConfig { |
| 203 | + key: root_key, |
| 204 | + }), |
| 205 | + pallet_babe: Some(BabeConfig { |
| 206 | + authorities: vec![], |
| 207 | + }), |
| 208 | + pallet_im_online: Some(ImOnlineConfig { |
| 209 | + keys: vec![], |
| 210 | + }), |
| 211 | + pallet_authority_discovery: Some(AuthorityDiscoveryConfig { |
| 212 | + keys: vec![], |
| 213 | + }), |
| 214 | + pallet_grandpa: Some(GrandpaConfig { |
| 215 | + authorities: vec![], |
| 216 | + }), |
| 217 | + pallet_membership_Instance1: Some(Default::default()), |
| 218 | + } |
| 219 | +} |
0 commit comments