referrerpolicy=no-referrer-when-downgrade

pallet_staking_async_parachain_runtime/
genesis_config_presets.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Staking Async Runtime genesis config presets
19
20use crate::*;
21use alloc::{
22	string::{String, ToString},
23	vec,
24	vec::Vec,
25};
26use cumulus_primitives_core::ParaId;
27use frame_support::build_struct_json_patch;
28use parachains_common::{AccountId, AuraId};
29use sp_core::{crypto::get_public_from_string_or_panic, sr25519};
30use sp_genesis_builder::PresetId;
31use sp_keyring::Sr25519Keyring;
32use sp_staking::StakerStatus;
33use testnet_parachains_constants::westend::{
34	currency::UNITS as WND, xcm_version::SAFE_XCM_VERSION,
35};
36
37const STAKING_ASYNC_PARA_ED: Balance = ExistentialDeposit::get();
38
39struct GenesisParams {
40	invulnerables: Vec<(AccountId, AuraId)>,
41	endowed_accounts: Vec<AccountId>,
42	endowment: Balance,
43	dev_stakers: Option<(u32, u32)>,
44	validators: Vec<AccountId>,
45	validator_count: u32,
46	root: AccountId,
47	id: ParaId,
48}
49
50fn staking_async_parachain_genesis(params: GenesisParams, preset: String) -> serde_json::Value {
51	let GenesisParams {
52		invulnerables,
53		endowed_accounts,
54		endowment,
55		dev_stakers,
56		validators,
57		validator_count,
58		root,
59		id,
60	} = params;
61	build_struct_json_patch!(RuntimeGenesisConfig {
62		balances: BalancesConfig {
63			balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
64		},
65		parachain_info: ParachainInfoConfig { parachain_id: id },
66		collator_selection: CollatorSelectionConfig {
67			invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
68			candidacy_bond: STAKING_ASYNC_PARA_ED * 16,
69		},
70		session: SessionConfig {
71			keys: invulnerables
72				.clone()
73				.into_iter()
74				.map(|(acc, aura)| {
75					(
76						acc.clone(),          // account id
77						acc,                  // validator id
78						SessionKeys { aura }, // session keys
79					)
80				})
81				.collect(),
82		},
83		polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
84		sudo: SudoConfig { key: Some(root) },
85		preset_store: crate::PresetStoreConfig { preset, ..Default::default() },
86		staking: StakingConfig {
87			validator_count,
88			dev_stakers,
89			stakers: validators
90				.into_iter()
91				.map(|acc| (acc, endowment / 2, StakerStatus::Validator))
92				.collect(),
93			..Default::default()
94		}
95	})
96}
97
98/// Provides the JSON representation of predefined genesis config for given `id`.
99pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
100	let mut params = GenesisParams {
101		invulnerables: vec![
102			// in all cases, our local collators is just charlie. ZN seems to override these
103			// anyways.
104			(
105				get_public_from_string_or_panic::<sr25519::Public>("Charlie").into(),
106				get_public_from_string_or_panic::<AuraId>("Charlie").into(),
107			),
108		],
109		endowed_accounts: Sr25519Keyring::well_known().map(|k| k.to_account_id()).collect(),
110		endowment: WND * 1_000_000,
111		dev_stakers: Some((100, 2000)),
112		validators: Default::default(),
113		validator_count: 10,
114		root: Sr25519Keyring::Alice.to_account_id(),
115		id: 1100.into(),
116	};
117	let patch = match id.as_ref() {
118		"real-s" => {
119			params.validator_count = 2;
120			// generate no new "fake" validators.
121			params.dev_stakers = Some((0, 500));
122			// set expected relay validators in genesis so they are elected
123			params.validators = vec![
124				Sr25519Keyring::AliceStash.to_account_id(),
125				Sr25519Keyring::BobStash.to_account_id(),
126			];
127			staking_async_parachain_genesis(params, id.to_string())
128		},
129		"real-m" => {
130			params.validator_count = 4;
131			// generate no new "fake" validators.
132			params.dev_stakers = Some((0, 2000));
133			// set expected relay validators in genesis so they are elected
134			params.validators = vec![
135				Sr25519Keyring::AliceStash.to_account_id(),
136				Sr25519Keyring::BobStash.to_account_id(),
137				Sr25519Keyring::EveStash.to_account_id(),
138				Sr25519Keyring::DaveStash.to_account_id(),
139			];
140			staking_async_parachain_genesis(params, id.to_string())
141		},
142		"fake-dev" => {
143			// nada
144			staking_async_parachain_genesis(params, id.to_string())
145		},
146		"fake-dot" => {
147			params.validator_count = 500;
148			params.dev_stakers = Some((2_500, 25_000));
149			staking_async_parachain_genesis(params, id.to_string())
150		},
151		"fake-ksm" => {
152			params.validator_count = 1_000;
153			params.dev_stakers = Some((4_500, 15_000));
154			staking_async_parachain_genesis(params, id.to_string())
155		},
156		_ => panic!("unrecognized genesis preset!"),
157	};
158
159	Some(
160		serde_json::to_string(&patch)
161			.expect("serialization to json is expected to work. qed.")
162			.into_bytes(),
163	)
164}
165
166/// List of supported presets.
167pub fn preset_names() -> Vec<PresetId> {
168	vec![
169		PresetId::from("real-s"),
170		PresetId::from("real-m"),
171		PresetId::from("fake-dev"),
172		PresetId::from("fake-dot"),
173		PresetId::from("fake-ksm"),
174	]
175}