partner_chains_demo_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
18use crate::{
19	AccountId, BalancesConfig, GovernedMapConfig, RuntimeGenesisConfig, SudoConfig,
20	test_helper_pallet,
21};
22use alloc::{vec, vec::Vec};
23use serde_json::Value;
24use sp_consensus_aura::sr25519::AuthorityId as AuraId;
25use sp_consensus_grandpa::AuthorityId as GrandpaId;
26use sp_genesis_builder::{self, PresetId};
27use sp_keyring::Sr25519Keyring;
28
29// Returns the genesis config presets populated with given parameters.
30fn testnet_genesis(
31	initial_authorities: Vec<(AuraId, GrandpaId)>,
32	endowed_accounts: Vec<AccountId>,
33	root: AccountId,
34) -> Value {
35	let config = RuntimeGenesisConfig {
36		balances: BalancesConfig {
37			balances: endowed_accounts
38				.iter()
39				.cloned()
40				.map(|k| (k, 1u128 << 60))
41				.collect::<Vec<_>>(),
42			dev_accounts: None,
43		},
44		aura: pallet_aura::GenesisConfig {
45			authorities: initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
46		},
47		grandpa: pallet_grandpa::GenesisConfig {
48			authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect::<Vec<_>>(),
49			..Default::default()
50		},
51		sudo: SudoConfig { key: Some(root) },
52		test_helper_pallet: test_helper_pallet::GenesisConfig {
53			participation_data_release_period:
54				test_helper_pallet::DEFAULT_PARTICIPATION_DATA_RELEASE_PERIOD,
55			_phantom: core::marker::PhantomData,
56		},
57		system: Default::default(),
58		transaction_payment: Default::default(),
59		sidechain: Default::default(),
60		session_committee_management: Default::default(),
61		pallet_session: Default::default(),
62		session: Default::default(),
63		native_token_management: Default::default(),
64		governed_map: GovernedMapConfig {
65			main_chain_scripts: Some(sp_governed_map::MainChainScriptsV1::default()),
66			..Default::default()
67		},
68		bridge: Default::default(),
69	};
70
71	serde_json::to_value(config).expect("Could not build genesis config.")
72}
73
74/// Return the development genesis config.
75pub fn development_config_genesis() -> Value {
76	testnet_genesis(
77		vec![(
78			sp_keyring::Sr25519Keyring::Alice.public().into(),
79			sp_keyring::Ed25519Keyring::Alice.public().into(),
80		)],
81		vec![
82			Sr25519Keyring::Alice.to_account_id(),
83			Sr25519Keyring::Bob.to_account_id(),
84			Sr25519Keyring::AliceStash.to_account_id(),
85			Sr25519Keyring::BobStash.to_account_id(),
86		],
87		sp_keyring::Sr25519Keyring::Alice.to_account_id(),
88	)
89}
90
91/// Return the local genesis config preset.
92pub fn local_config_genesis() -> Value {
93	testnet_genesis(
94		vec![
95			(
96				sp_keyring::Sr25519Keyring::Alice.public().into(),
97				sp_keyring::Ed25519Keyring::Alice.public().into(),
98			),
99			(
100				sp_keyring::Sr25519Keyring::Bob.public().into(),
101				sp_keyring::Ed25519Keyring::Bob.public().into(),
102			),
103		],
104		Sr25519Keyring::iter()
105			.filter(|v| v != &Sr25519Keyring::One && v != &Sr25519Keyring::Two)
106			.map(|v| v.to_account_id())
107			.collect::<Vec<_>>(),
108		Sr25519Keyring::Alice.to_account_id(),
109	)
110}
111
112/// Provides the JSON representation of predefined genesis config for given `id`.
113pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
114	let patch = match id.as_ref() {
115		sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(),
116		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => local_config_genesis(),
117		_ => return None,
118	};
119	Some(
120		serde_json::to_string(&patch)
121			.expect("serialization to json is expected to work. qed.")
122			.into_bytes(),
123	)
124}
125
126/// List of supported presets.
127pub fn preset_names() -> Vec<PresetId> {
128	vec![
129		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
130		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
131	]
132}