referrerpolicy=no-referrer-when-downgrade

parachain_template_runtime/
genesis_config_presets.rs

1use crate::{
2	AccountId, BalancesConfig, CollatorSelectionConfig, ParachainInfoConfig, PolkadotXcmConfig,
3	RuntimeGenesisConfig, SessionConfig, SessionKeys, SudoConfig, EXISTENTIAL_DEPOSIT,
4};
5
6use alloc::{vec, vec::Vec};
7
8use polkadot_sdk::{staging_xcm as xcm, *};
9
10use cumulus_primitives_core::ParaId;
11use frame_support::build_struct_json_patch;
12use parachains_common::AuraId;
13use serde_json::Value;
14use sp_genesis_builder::PresetId;
15use sp_keyring::Sr25519Keyring;
16
17/// The default XCM version to set in genesis config.
18const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
19/// Parachain id used for genesis config presets of parachain template.
20#[docify::export_content]
21pub const PARACHAIN_ID: u32 = 1000;
22
23/// Generate the session keys from individual elements.
24///
25/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
26pub fn template_session_keys(keys: AuraId) -> SessionKeys {
27	SessionKeys { aura: keys }
28}
29
30fn testnet_genesis(
31	invulnerables: Vec<(AccountId, AuraId)>,
32	endowed_accounts: Vec<AccountId>,
33	root: AccountId,
34	id: ParaId,
35) -> Value {
36	build_struct_json_patch!(RuntimeGenesisConfig {
37		balances: BalancesConfig {
38			balances: endowed_accounts
39				.iter()
40				.cloned()
41				.map(|k| (k, 1u128 << 60))
42				.collect::<Vec<_>>(),
43		},
44		parachain_info: ParachainInfoConfig { parachain_id: id },
45		collator_selection: CollatorSelectionConfig {
46			invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect::<Vec<_>>(),
47			candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
48		},
49		session: SessionConfig {
50			keys: invulnerables
51				.into_iter()
52				.map(|(acc, aura)| {
53					(
54						acc.clone(),                 // account id
55						acc,                         // validator id
56						template_session_keys(aura), // session keys
57					)
58				})
59				.collect::<Vec<_>>(),
60		},
61		polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
62		sudo: SudoConfig { key: Some(root) },
63	})
64}
65
66fn local_testnet_genesis() -> Value {
67	testnet_genesis(
68		// initial collators.
69		vec![
70			(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
71			(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
72		],
73		Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
74		Sr25519Keyring::Alice.to_account_id(),
75		PARACHAIN_ID.into(),
76	)
77}
78
79fn development_config_genesis() -> Value {
80	testnet_genesis(
81		// initial collators.
82		vec![
83			(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
84			(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
85		],
86		Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
87		Sr25519Keyring::Alice.to_account_id(),
88		PARACHAIN_ID.into(),
89	)
90}
91
92/// Provides the JSON representation of predefined genesis config for given `id`.
93pub fn get_preset(id: &PresetId) -> Option<vec::Vec<u8>> {
94	let patch = match id.as_ref() {
95		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => local_testnet_genesis(),
96		sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(),
97		_ => return None,
98	};
99	Some(
100		serde_json::to_string(&patch)
101			.expect("serialization to json is expected to work. qed.")
102			.into_bytes(),
103	)
104}
105
106/// List of supported presets.
107pub fn preset_names() -> Vec<PresetId> {
108	vec![
109		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
110		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
111	]
112}