|
| 1 | +// This file is part of Acala. |
| 2 | + |
| 3 | +// Copyright (C) 2020-2025 Acala Foundation. |
| 4 | +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
| 5 | + |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU General Public License as published by |
| 8 | +// the Free Software Foundation, either version 3 of the License, or |
| 9 | +// (at your option) any later version. |
| 10 | + |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | + |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +use crate::{ |
| 20 | + AccountId, BalancesConfig, CollatorSelectionConfig, NativeTokenExistentialDeposit, ParachainInfoConfig, |
| 21 | + PolkadotXcmConfig, RuntimeGenesisConfig, SessionConfig, SessionKeys, SudoConfig, XCM_VERSION, |
| 22 | +}; |
| 23 | + |
| 24 | +use alloc::{vec, vec::Vec}; |
| 25 | + |
| 26 | +use cumulus_primitives_core::ParaId; |
| 27 | +use frame_support::build_struct_json_patch; |
| 28 | +use parachains_common::AuraId; |
| 29 | +use serde_json::Value; |
| 30 | +use sp_genesis_builder::PresetId; |
| 31 | +use sp_keyring::Sr25519Keyring; |
| 32 | +use xcm::prelude::XCM_VERSION; |
| 33 | + |
| 34 | +/// The default XCM version to set in genesis config. |
| 35 | +const SAFE_XCM_VERSION: u32 = XCM_VERSION; |
| 36 | +/// Parachain id used for genesis config presets of parachain template. |
| 37 | +pub const PARACHAIN_ID: u32 = 2000; |
| 38 | + |
| 39 | +/// Generate the session keys from individual elements. |
| 40 | +/// |
| 41 | +/// The input must be a tuple of individual keys (a single arg for now since we have just one key). |
| 42 | +pub fn template_session_keys(keys: AuraId) -> SessionKeys { |
| 43 | + SessionKeys { aura: keys } |
| 44 | +} |
| 45 | + |
| 46 | +fn testnet_genesis( |
| 47 | + invulnerables: Vec<(AccountId, AuraId)>, |
| 48 | + endowed_accounts: Vec<AccountId>, |
| 49 | + root: AccountId, |
| 50 | + id: ParaId, |
| 51 | +) -> Value { |
| 52 | + build_struct_json_patch!(RuntimeGenesisConfig { |
| 53 | + balances: BalancesConfig { |
| 54 | + balances: endowed_accounts |
| 55 | + .iter() |
| 56 | + .cloned() |
| 57 | + .map(|k| (k, 1u128 << 60)) |
| 58 | + .collect::<Vec<_>>(), |
| 59 | + }, |
| 60 | + parachain_info: ParachainInfoConfig { parachain_id: id }, |
| 61 | + collator_selection: CollatorSelectionConfig { |
| 62 | + invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect::<Vec<_>>(), |
| 63 | + candidacy_bond: NativeTokenExistentialDeposit::get() * 16, |
| 64 | + }, |
| 65 | + session: SessionConfig { |
| 66 | + keys: invulnerables |
| 67 | + .into_iter() |
| 68 | + .map(|(acc, aura)| { |
| 69 | + ( |
| 70 | + acc.clone(), // account id |
| 71 | + acc, // validator id |
| 72 | + template_session_keys(aura), // session keys |
| 73 | + ) |
| 74 | + }) |
| 75 | + .collect::<Vec<_>>(), |
| 76 | + }, |
| 77 | + polkadot_xcm: PolkadotXcmConfig { |
| 78 | + safe_xcm_version: Some(SAFE_XCM_VERSION) |
| 79 | + }, |
| 80 | + sudo: SudoConfig { key: Some(root) }, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +fn local_testnet_genesis() -> Value { |
| 85 | + testnet_genesis( |
| 86 | + // initial collators. |
| 87 | + vec![ |
| 88 | + ( |
| 89 | + Sr25519Keyring::Alice.to_account_id(), |
| 90 | + Sr25519Keyring::Alice.public().into(), |
| 91 | + ), |
| 92 | + (Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()), |
| 93 | + ], |
| 94 | + Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(), |
| 95 | + Sr25519Keyring::Alice.to_account_id(), |
| 96 | + PARACHAIN_ID.into(), |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +fn development_config_genesis() -> Value { |
| 101 | + testnet_genesis( |
| 102 | + // initial collators. |
| 103 | + vec![ |
| 104 | + ( |
| 105 | + Sr25519Keyring::Alice.to_account_id(), |
| 106 | + Sr25519Keyring::Alice.public().into(), |
| 107 | + ), |
| 108 | + (Sr25519Keyring::Bob.to_account_id(), Sr25519Keyring::Bob.public().into()), |
| 109 | + ], |
| 110 | + Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(), |
| 111 | + Sr25519Keyring::Alice.to_account_id(), |
| 112 | + PARACHAIN_ID.into(), |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +/// Provides the JSON representation of predefined genesis config for given `id`. |
| 117 | +pub fn get_preset(id: &PresetId) -> Option<vec::Vec<u8>> { |
| 118 | + let patch = match id.as_ref() { |
| 119 | + sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => local_testnet_genesis(), |
| 120 | + sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(), |
| 121 | + _ => return None, |
| 122 | + }; |
| 123 | + Some( |
| 124 | + serde_json::to_string(&patch) |
| 125 | + .expect("serialization to json is expected to work. qed.") |
| 126 | + .into_bytes(), |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +/// List of supported presets. |
| 131 | +pub fn preset_names() -> Vec<PresetId> { |
| 132 | + vec![ |
| 133 | + PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET), |
| 134 | + PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET), |
| 135 | + ] |
| 136 | +} |
0 commit comments