referrerpolicy=no-referrer-when-downgrade

penpal_runtime/
genesis_config_presets.rs

1// This file is part of Cumulus.
2// SPDX-License-Identifier: Unlicense
3
4// This is free and unencumbered software released into the public domain.
5
6// Anyone is free to copy, modify, publish, use, compile, sell, or
7// distribute this software, either in source code form or as a compiled
8// binary, for any purpose, commercial or non-commercial, and by any
9// means.
10
11// In jurisdictions that recognize copyright laws, the author or authors
12// of this software dedicate any and all copyright interest in the
13// software to the public domain. We make this dedication for the benefit
14// of the public at large and to the detriment of our heirs and
15// successors. We intend this dedication to be an overt act of
16// relinquishment in perpetuity of all present and future rights to this
17// software under copyright law.
18
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25// OTHER DEALINGS IN THE SOFTWARE.
26
27// For more information, please refer to <http://unlicense.org/>
28
29//! Penpal Parachain Runtime genesis config presets
30
31use crate::*;
32use alloc::{vec, vec::Vec};
33use cumulus_primitives_core::ParaId;
34use frame_support::build_struct_json_patch;
35use parachains_common::{AccountId, AuraId};
36use sp_genesis_builder::PresetId;
37use sp_keyring::Sr25519Keyring;
38
39const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
40
41const DEFAULT_PARA_ID: ParaId = ParaId::new(2000);
42const ENDOWMENT: u128 = 1 << 60;
43
44fn penpal_parachain_genesis(
45	sudo: AccountId,
46	invulnerables: Vec<(AccountId, AuraId)>,
47	endowed_accounts: Vec<AccountId>,
48	endowment: Balance,
49	id: ParaId,
50) -> serde_json::Value {
51	build_struct_json_patch!(RuntimeGenesisConfig {
52		balances: BalancesConfig {
53			balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
54		},
55		parachain_info: ParachainInfoConfig { parachain_id: id },
56		collator_selection: CollatorSelectionConfig {
57			invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
58			candidacy_bond: crate::EXISTENTIAL_DEPOSIT * 16,
59		},
60		session: SessionConfig {
61			keys: invulnerables
62				.into_iter()
63				.map(|(acc, aura)| {
64					(
65						acc.clone(),               // account id
66						acc,                       // validator id
67						penpal_session_keys(aura), // session keys
68					)
69				})
70				.collect(),
71		},
72		polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
73		sudo: SudoConfig { key: Some(sudo.clone()) },
74		assets: AssetsConfig {
75			assets: vec![(
76				crate::xcm_config::TELEPORTABLE_ASSET_ID,
77				sudo.clone(), // owner
78				false,        // is_sufficient
79				crate::EXISTENTIAL_DEPOSIT,
80			)],
81			metadata: vec![(
82				crate::xcm_config::TELEPORTABLE_ASSET_ID,
83				"pal-2".as_bytes().to_vec(),
84				"pal-2".as_bytes().to_vec(),
85				12,
86			)],
87			accounts: vec![(
88				crate::xcm_config::TELEPORTABLE_ASSET_ID,
89				sudo.clone(),
90				crate::EXISTENTIAL_DEPOSIT * 4096,
91			)]
92		},
93		foreign_assets: ForeignAssetsConfig {
94			assets: vec![(
95				crate::xcm_config::RelayLocation::get(),
96				sudo.clone(),
97				true,
98				crate::EXISTENTIAL_DEPOSIT
99			)],
100			metadata: vec![(
101				crate::xcm_config::RelayLocation::get(),
102				"relay".as_bytes().to_vec(),
103				"relay".as_bytes().to_vec(),
104				12
105			)],
106			accounts: vec![(
107				crate::xcm_config::RelayLocation::get(),
108				sudo,
109				crate::EXISTENTIAL_DEPOSIT * 4096,
110			)]
111		}
112	})
113}
114
115/// Provides the JSON representation of predefined genesis config for given `id`.
116pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
117	let genesis_fn = |authorities| {
118		penpal_parachain_genesis(
119			Sr25519Keyring::Alice.to_account_id(),
120			authorities,
121			Sr25519Keyring::well_known().map(|x| x.to_account_id()).collect(),
122			ENDOWMENT,
123			DEFAULT_PARA_ID,
124		)
125	};
126
127	let patch = match id.as_ref() {
128		sp_genesis_builder::DEV_RUNTIME_PRESET => genesis_fn(vec![(
129			Sr25519Keyring::Alice.to_account_id(),
130			Sr25519Keyring::Alice.public().into(),
131		)]),
132		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => genesis_fn(vec![
133			(Sr25519Keyring::Alice.to_account_id(), Sr25519Keyring::Alice.public().into()),
134			(Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()),
135		]),
136		_ => return None,
137	};
138
139	Some(
140		serde_json::to_string(&patch)
141			.expect("serialization to json is expected to work. qed.")
142			.into_bytes(),
143	)
144}
145
146/// List of supported presets.
147pub fn preset_names() -> Vec<PresetId> {
148	vec![
149		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
150		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
151	]
152}
153
154/// Generate the session keys from individual elements.
155///
156/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
157pub fn penpal_session_keys(keys: AuraId) -> crate::SessionKeys {
158	crate::SessionKeys { aura: keys }
159}