1use crate::{
20 BabeConfig, BalancesConfig, ConfigurationConfig, RegistrarConfig, RuntimeGenesisConfig,
21 SessionConfig, SessionKeys, SudoConfig, BABE_GENESIS_EPOCH_CONFIG,
22};
23#[cfg(not(feature = "std"))]
24use alloc::format;
25use alloc::{vec, vec::Vec};
26use frame_support::build_struct_json_patch;
27use polkadot_primitives::{AccountId, AssignmentId, SchedulerParams, ValidatorId};
28use rococo_runtime_constants::currency::UNITS as ROC;
29use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
30use sp_consensus_babe::AuthorityId as BabeId;
31use sp_consensus_beefy::ecdsa_crypto::AuthorityId as BeefyId;
32use sp_consensus_grandpa::AuthorityId as GrandpaId;
33use sp_core::{crypto::get_public_from_string_or_panic, sr25519};
34use sp_genesis_builder::PresetId;
35use sp_keyring::Sr25519Keyring;
36
37fn get_authority_keys_from_seed(
39 seed: &str,
40) -> (
41 AccountId,
42 AccountId,
43 BabeId,
44 GrandpaId,
45 ValidatorId,
46 AssignmentId,
47 AuthorityDiscoveryId,
48 BeefyId,
49) {
50 let keys = get_authority_keys_from_seed_no_beefy(seed);
51 (
52 keys.0,
53 keys.1,
54 keys.2,
55 keys.3,
56 keys.4,
57 keys.5,
58 keys.6,
59 get_public_from_string_or_panic::<BeefyId>(seed),
60 )
61}
62
63fn get_authority_keys_from_seed_no_beefy(
65 seed: &str,
66) -> (AccountId, AccountId, BabeId, GrandpaId, ValidatorId, AssignmentId, AuthorityDiscoveryId) {
67 (
68 get_public_from_string_or_panic::<sr25519::Public>(&format!("{}//stash", seed)).into(),
69 get_public_from_string_or_panic::<sr25519::Public>(seed).into(),
70 get_public_from_string_or_panic::<BabeId>(seed),
71 get_public_from_string_or_panic::<GrandpaId>(seed),
72 get_public_from_string_or_panic::<ValidatorId>(seed),
73 get_public_from_string_or_panic::<AssignmentId>(seed),
74 get_public_from_string_or_panic::<AuthorityDiscoveryId>(seed),
75 )
76}
77
78fn testnet_accounts() -> Vec<AccountId> {
79 Sr25519Keyring::well_known().map(|x| x.to_account_id()).collect()
80}
81
82fn rococo_session_keys(
83 babe: BabeId,
84 grandpa: GrandpaId,
85 para_validator: ValidatorId,
86 para_assignment: AssignmentId,
87 authority_discovery: AuthorityDiscoveryId,
88 beefy: BeefyId,
89) -> SessionKeys {
90 SessionKeys { babe, grandpa, para_validator, para_assignment, authority_discovery, beefy }
91}
92
93fn default_parachains_host_configuration(
94) -> polkadot_runtime_parachains::configuration::HostConfiguration<polkadot_primitives::BlockNumber>
95{
96 use polkadot_primitives::{
97 node_features::FeatureIndex, AsyncBackingParams, MAX_CODE_SIZE, MAX_POV_SIZE,
98 };
99
100 polkadot_runtime_parachains::configuration::HostConfiguration {
101 validation_upgrade_cooldown: 2u32,
102 validation_upgrade_delay: 2,
103 code_retention_period: 1200,
104 max_code_size: MAX_CODE_SIZE,
105 max_pov_size: MAX_POV_SIZE,
106 max_head_data_size: 32 * 1024,
107 max_upward_queue_count: 8,
108 max_upward_queue_size: 1024 * 1024,
109 max_downward_message_size: 1024 * 1024,
110 max_upward_message_size: 50 * 1024,
111 max_upward_message_num_per_candidate: 5,
112 hrmp_sender_deposit: 0,
113 hrmp_recipient_deposit: 0,
114 hrmp_channel_max_capacity: 8,
115 hrmp_channel_max_total_size: 8 * 1024,
116 hrmp_max_parachain_inbound_channels: 4,
117 hrmp_channel_max_message_size: 1024 * 1024,
118 hrmp_max_parachain_outbound_channels: 4,
119 hrmp_max_message_num_per_candidate: 5,
120 dispute_period: 6,
121 no_show_slots: 2,
122 n_delay_tranches: 25,
123 needed_approvals: 2,
124 relay_vrf_modulo_samples: 2,
125 zeroth_delay_tranche_width: 0,
126 minimum_validation_upgrade_delay: 5,
127 async_backing_params: AsyncBackingParams {
128 max_candidate_depth: 0,
129 allowed_ancestry_len: 0,
130 },
131 node_features: bitvec::vec::BitVec::from_element(
132 (1u8 << (FeatureIndex::ElasticScalingMVP as usize)) |
133 (1u8 << (FeatureIndex::EnableAssignmentsV2 as usize)) |
134 (1u8 << (FeatureIndex::CandidateReceiptV2 as usize)),
135 ),
136 scheduler_params: SchedulerParams {
137 lookahead: 3,
138 group_rotation_frequency: 20,
139 paras_availability_period: 4,
140 ..Default::default()
141 },
142 ..Default::default()
143 }
144}
145
146#[test]
147fn default_parachains_host_configuration_is_consistent() {
148 default_parachains_host_configuration().panic_if_not_consistent();
149}
150
151fn rococo_testnet_genesis(
152 initial_authorities: Vec<(
153 AccountId,
154 AccountId,
155 BabeId,
156 GrandpaId,
157 ValidatorId,
158 AssignmentId,
159 AuthorityDiscoveryId,
160 BeefyId,
161 )>,
162 root_key: AccountId,
163 endowed_accounts: Option<Vec<AccountId>>,
164) -> serde_json::Value {
165 let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(testnet_accounts);
166
167 const ENDOWMENT: u128 = 1_000_000 * ROC;
168
169 build_struct_json_patch!(RuntimeGenesisConfig {
170 balances: BalancesConfig {
171 balances: endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
172 },
173 session: SessionConfig {
174 keys: initial_authorities
175 .iter()
176 .map(|x| {
177 (
178 x.0.clone(),
179 x.0.clone(),
180 rococo_session_keys(
181 x.2.clone(),
182 x.3.clone(),
183 x.4.clone(),
184 x.5.clone(),
185 x.6.clone(),
186 x.7.clone(),
187 ),
188 )
189 })
190 .collect::<Vec<_>>(),
191 },
192 babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG },
193 sudo: SudoConfig { key: Some(root_key.clone()) },
194 configuration: ConfigurationConfig {
195 config: polkadot_runtime_parachains::configuration::HostConfiguration {
196 scheduler_params: SchedulerParams {
197 max_validators_per_core: Some(1),
198 ..default_parachains_host_configuration().scheduler_params
199 },
200 ..default_parachains_host_configuration()
201 },
202 },
203 registrar: RegistrarConfig { next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID },
204 })
205}
206
207fn rococo_staging_testnet_config_genesis() -> serde_json::Value {
209 use hex_literal::hex;
210 use sp_core::crypto::UncheckedInto;
211
212 let endowed_accounts = Vec::from([
214 hex!["52bc71c1eca5353749542dfdf0af97bf764f9c2f44e860cd485f1cd86400f649"].into(),
216 ]);
217
218 let initial_authorities: Vec<(
220 AccountId,
221 AccountId,
222 BabeId,
223 GrandpaId,
224 ValidatorId,
225 AssignmentId,
226 AuthorityDiscoveryId,
227 BeefyId,
228 )> = Vec::from([
229 (
230 hex!["62475fe5406a7cb6a64c51d0af9d3ab5c2151bcae982fb812f7a76b706914d6a"].into(),
232 hex!["9e6e781a76810fe93187af44c79272c290c2b9e2b8b92ee11466cd79d8023f50"].into(),
234 hex!["a076ef1280d768051f21d060623da3ab5b56944d681d303ed2d4bf658c5bed35"]
236 .unchecked_into(),
237 hex!["0e6d7d1afbcc6547b92995a394ba0daed07a2420be08220a5a1336c6731f0bfa"]
239 .unchecked_into(),
240 hex!["0e07a51d3213842f8e9363ce8e444255990a225f87e80a3d651db7841e1a0205"]
242 .unchecked_into(),
243 hex!["ec60e71fe4a567ef9fef99d4bbf37ffae70564b41aa6f94ef0317c13e0a5477b"]
245 .unchecked_into(),
246 hex!["f49eae66a0ac9f610316906ec8f1a0928e20d7059d76a5ca53cbcb5a9b50dd3c"]
248 .unchecked_into(),
249 hex!["034f68c5661a41930c82f26a662276bf89f33467e1c850f2fb8ef687fe43d62276"]
251 .unchecked_into(),
252 ),
253 (
254 hex!["520b48452969f6ddf263b664de0adb0c729d0e0ad3b0e5f3cb636c541bc9022a"].into(),
256 hex!["6618289af7ae8621981ffab34591e7a6486e12745dfa3fd3b0f7e6a3994c7b5b"].into(),
258 hex!["38757d0de00a0c739e7d7984ef4bc01161bd61e198b7c01b618425c16bb5bd5f"]
260 .unchecked_into(),
261 hex!["fcd5f87a6fd5707a25122a01b4dac0a8482259df7d42a9a096606df1320df08d"]
263 .unchecked_into(),
264 hex!["669a10892119453e9feb4e3f1ee8e028916cc3240022920ad643846fbdbee816"]
266 .unchecked_into(),
267 hex!["68bf52c482630a8d1511f2edd14f34127a7d7082219cccf7fd4c6ecdb535f80d"]
269 .unchecked_into(),
270 hex!["f6f8fe475130d21165446a02fb1dbce3a7bf36412e5d98f4f0473aed9252f349"]
272 .unchecked_into(),
273 hex!["03a90c2bb6d3b7000020f6152fe2e5002fa970fd1f42aafb6c8edda8dacc2ea77e"]
275 .unchecked_into(),
276 ),
277 (
278 hex!["92ef83665b39d7a565e11bf8d18d41d45a8011601c339e57a8ea88c8ff7bba6f"].into(),
280 hex!["b235f57244230589523271c27b8a490922ffd7dccc83b044feaf22273c1dc735"].into(),
282 hex!["d2644c1ab2c63a3ad8d40ad70d4b260969e3abfe6d7e6665f50dc9f6365c9d2a"]
284 .unchecked_into(),
285 hex!["e1b68fbd84333e31486c08e6153d9a1415b2e7e71b413702b7d64e9b631184a1"]
287 .unchecked_into(),
288 hex!["a8e61ffacafaf546283dc92d14d7cc70ea0151a5dd81fdf73ff5a2951f2b6037"]
290 .unchecked_into(),
291 hex!["244f3421b310c68646e99cdbf4963e02067601f57756b072a4b19431448c186e"]
293 .unchecked_into(),
294 hex!["2c57f81fd311c1ab53813c6817fe67f8947f8d39258252663b3384ab4195494d"]
296 .unchecked_into(),
297 hex!["039d065fe4f9234f0a4f13cc3ae585f2691e9c25afa469618abb6645111f607a53"]
299 .unchecked_into(),
300 ),
301 (
302 hex!["38f3c2f38f6d47f161e98c697bbe3ca0e47c033460afda0dda314ab4222a0404"].into(),
304 hex!["ba0898c1964196474c0be08d364cdf4e9e1d47088287f5235f70b0590dfe1704"].into(),
306 hex!["764186bc30fd5a02477f19948dc723d6d57ab174debd4f80ed6038ec960bfe21"]
308 .unchecked_into(),
309 hex!["36be9069cdb4a8a07ecd51f257875150f0a8a1be44a10d9d98dabf10a030aef4"]
311 .unchecked_into(),
312 hex!["882d72965e642677583b333b2d173ac94b5fd6c405c76184bb14293be748a13b"]
314 .unchecked_into(),
315 hex!["821271c99c958b9220f1771d9f5e29af969edfa865631dba31e1ab7bc0582b75"]
317 .unchecked_into(),
318 hex!["2496f28d887d84705c6dae98aee8bf90fc5ad10bb5545eca1de6b68425b70f7c"]
320 .unchecked_into(),
321 hex!["0307d29bbf6a5c4061c2157b44fda33b7bb4ec52a5a0305668c74688cedf288d58"]
323 .unchecked_into(),
324 ),
325 (
326 hex!["02a2d8cfcf75dda85fafc04ace3bcb73160034ed1964c43098fb1fe831de1b16"].into(),
328 hex!["90cab33f0bb501727faa8319f0845faef7d31008f178b65054b6629fe531b772"].into(),
330 hex!["7c94715e5dd8ab54221b1b6b2bfa5666f593f28a92a18e28052531de1bd80813"]
332 .unchecked_into(),
333 hex!["6c878e33b83c20324238d22240f735457b6fba544b383e70bb62a27b57380c81"]
335 .unchecked_into(),
336 hex!["6a8570b9c6408e54bacf123cc2bb1b0f087f9c149147d0005badba63a5a4ac01"]
338 .unchecked_into(),
339 hex!["16c69ea8d595e80b6736f44be1eaeeef2ac9c04a803cc4fd944364cb0d617a33"]
341 .unchecked_into(),
342 hex!["306ac5c772fe858942f92b6e28bd82fb7dd8cdd25f9a4626c1b0eee075fcb531"]
344 .unchecked_into(),
345 hex!["02fb0330356e63a35dd930bc74525edf28b3bf5eb44aab9e9e4962c8309aaba6a6"]
347 .unchecked_into(),
348 ),
349 (
350 hex!["02ea6bfa8b23b92fe4b5db1063a1f9475e3acd0ab61e6b4f454ed6ba00b5f864"].into(),
352 hex!["d4ffc4c05b47d1115ad200f7f86e307b20b46c50e1b72a912ec4f6f7db46b616"].into(),
354 hex!["bab3cccdcc34401e9b3971b96a662686cf755aa869a5c4b762199ce531b12c5b"]
356 .unchecked_into(),
357 hex!["d9c056c98ca0e6b4eb7f5c58c007c1db7be0fe1f3776108f797dd4990d1ccc33"]
359 .unchecked_into(),
360 hex!["1efc23c0b51ad609ab670ecf45807e31acbd8e7e5cb7c07cf49ee42992d2867c"]
362 .unchecked_into(),
363 hex!["4c64d3f06d28adeb36a892fdaccecace150bec891f04694448a60b74fa469c22"]
365 .unchecked_into(),
366 hex!["160ea09c5717270e958a3da42673fa011613a9539b2e4ebcad8626bc117ca04a"]
368 .unchecked_into(),
369 hex!["020019a8bb188f8145d02fa855e9c36e9914457d37c500e03634b5223aa5702474"]
371 .unchecked_into(),
372 ),
373 (
374 hex!["fa373e25a1c4fe19c7148acde13bc3db1811cf656dc086820f3dda736b9c4a00"].into(),
376 hex!["62145d721967bd88622d08625f0f5681463c0f1b8bcd97eb3c2c53f7660fd513"].into(),
378 hex!["720537e2c1c554654d73b3889c3ef4c3c2f95a65dd3f7c185ebe4afebed78372"]
380 .unchecked_into(),
381 hex!["4bea0b37e0cce9bddd80835fa2bfd5606f5dcfb8388bbb10b10c483f0856cf14"]
383 .unchecked_into(),
384 hex!["042f07fc5268f13c026bbe199d63e6ac77a0c2a780f71cda05cee5a6f1b3f11f"]
386 .unchecked_into(),
387 hex!["fab485e87ed1537d089df521edf983a777c57065a702d7ed2b6a2926f31da74f"]
389 .unchecked_into(),
390 hex!["64d59feddb3d00316a55906953fb3db8985797472bd2e6c7ea1ab730cc339d7f"]
392 .unchecked_into(),
393 hex!["033f1a6d47fe86f88934e4b83b9fae903b92b5dcf4fec97d5e3e8bf4f39df03685"]
395 .unchecked_into(),
396 ),
397 (
398 hex!["8062e9c21f1d92926103119f7e8153cebdb1e5ab3e52d6f395be80bb193eab47"].into(),
400 hex!["fa0388fa88f3f0cb43d583e2571fbc0edad57dff3a6fd89775451dd2c2b8ea00"].into(),
402 hex!["da6b2df18f0f9001a6dcf1d301b92534fe9b1f3ccfa10c49449fee93adaa8349"]
404 .unchecked_into(),
405 hex!["4ee66173993dd0db5d628c4c9cb61a27b76611ad3c3925947f0d0011ee2c5dcc"]
407 .unchecked_into(),
408 hex!["d822d4088b20dca29a580a577a97d6f024bb24c9550bebdfd7d2d18e946a1c7d"]
410 .unchecked_into(),
411 hex!["481538f8c2c011a76d7d57db11c2789a5e83b0f9680dc6d26211d2f9c021ae4c"]
413 .unchecked_into(),
414 hex!["4e262811acdfe94528bfc3c65036080426a0e1301b9ada8d687a70ffcae99c26"]
416 .unchecked_into(),
417 hex!["025e84e95ed043e387ddb8668176b42f8e2773ddd84f7f58a6d9bf436a4b527986"]
419 .unchecked_into(),
420 ),
421 ]);
422
423 const ENDOWMENT: u128 = 1_000_000 * ROC;
424 const STASH: u128 = 100 * ROC;
425
426 build_struct_json_patch!(RuntimeGenesisConfig {
427 balances: BalancesConfig {
428 balances: endowed_accounts
429 .iter()
430 .map(|k: &AccountId| (k.clone(), ENDOWMENT))
431 .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
432 .collect::<Vec<_>>(),
433 },
434 session: SessionConfig {
435 keys: initial_authorities
436 .into_iter()
437 .map(|x| (x.0.clone(), x.0, rococo_session_keys(x.2, x.3, x.4, x.5, x.6, x.7)))
438 .collect::<Vec<_>>(),
439 },
440 babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG },
441 sudo: SudoConfig { key: Some(endowed_accounts[0].clone()) },
442 configuration: ConfigurationConfig { config: default_parachains_host_configuration() },
443 registrar: RegistrarConfig { next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID },
444 })
445}
446
447fn rococo_development_config_genesis() -> serde_json::Value {
449 rococo_testnet_genesis(
450 Vec::from([get_authority_keys_from_seed("Alice")]),
451 Sr25519Keyring::Alice.to_account_id(),
452 None,
453 )
454}
455
456fn rococo_local_testnet_genesis() -> serde_json::Value {
458 rococo_testnet_genesis(
459 Vec::from([get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob")]),
460 Sr25519Keyring::Alice.to_account_id(),
461 None,
462 )
463}
464
465fn versi_local_testnet_genesis() -> serde_json::Value {
468 rococo_testnet_genesis(
469 Vec::from([
470 get_authority_keys_from_seed("Alice"),
471 get_authority_keys_from_seed("Bob"),
472 get_authority_keys_from_seed("Charlie"),
473 get_authority_keys_from_seed("Dave"),
474 ]),
475 Sr25519Keyring::Alice.to_account_id(),
476 None,
477 )
478}
479
480pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
482 let patch = match id.as_ref() {
483 sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => rococo_local_testnet_genesis(),
484 sp_genesis_builder::DEV_RUNTIME_PRESET => rococo_development_config_genesis(),
485 "staging_testnet" => rococo_staging_testnet_config_genesis(),
486 "versi_local_testnet" => versi_local_testnet_genesis(),
487 _ => return None,
488 };
489 Some(
490 serde_json::to_string(&patch)
491 .expect("serialization to json is expected to work. qed.")
492 .into_bytes(),
493 )
494}
495
496pub fn preset_names() -> Vec<PresetId> {
498 vec![
499 PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
500 PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
501 PresetId::from("staging_testnet"),
502 PresetId::from("versi_local_testnet"),
503 ]
504}