referrerpolicy=no-referrer-when-downgrade

solochain_template_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::{AccountId, BalancesConfig, RuntimeGenesisConfig, SudoConfig};
19use alloc::{vec, vec::Vec};
20use frame_support::build_struct_json_patch;
21use serde_json::Value;
22use sp_consensus_aura::sr25519::AuthorityId as AuraId;
23use sp_consensus_grandpa::AuthorityId as GrandpaId;
24use sp_genesis_builder::{self, PresetId};
25use sp_keyring::Sr25519Keyring;
26
27// Returns the genesis config presets populated with given parameters.
28fn testnet_genesis(
29	initial_authorities: Vec<(AuraId, GrandpaId)>,
30	endowed_accounts: Vec<AccountId>,
31	root: AccountId,
32) -> Value {
33	build_struct_json_patch!(RuntimeGenesisConfig {
34		balances: BalancesConfig {
35			balances: endowed_accounts
36				.iter()
37				.cloned()
38				.map(|k| (k, 1u128 << 60))
39				.collect::<Vec<_>>(),
40		},
41		aura: pallet_aura::GenesisConfig {
42			authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect::<Vec<_>>(),
43		},
44		grandpa: pallet_grandpa::GenesisConfig {
45			authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect::<Vec<_>>(),
46		},
47		sudo: SudoConfig { key: Some(root) },
48	})
49}
50
51/// Return the development genesis config.
52pub fn development_config_genesis() -> Value {
53	testnet_genesis(
54		vec![(
55			sp_keyring::Sr25519Keyring::Alice.public().into(),
56			sp_keyring::Ed25519Keyring::Alice.public().into(),
57		)],
58		vec![
59			Sr25519Keyring::Alice.to_account_id(),
60			Sr25519Keyring::Bob.to_account_id(),
61			Sr25519Keyring::AliceStash.to_account_id(),
62			Sr25519Keyring::BobStash.to_account_id(),
63		],
64		sp_keyring::Sr25519Keyring::Alice.to_account_id(),
65	)
66}
67
68/// Return the local genesis config preset.
69pub fn local_config_genesis() -> Value {
70	testnet_genesis(
71		vec![
72			(
73				sp_keyring::Sr25519Keyring::Alice.public().into(),
74				sp_keyring::Ed25519Keyring::Alice.public().into(),
75			),
76			(
77				sp_keyring::Sr25519Keyring::Bob.public().into(),
78				sp_keyring::Ed25519Keyring::Bob.public().into(),
79			),
80		],
81		Sr25519Keyring::iter()
82			.filter(|v| v != &Sr25519Keyring::One && v != &Sr25519Keyring::Two)
83			.map(|v| v.to_account_id())
84			.collect::<Vec<_>>(),
85		Sr25519Keyring::Alice.to_account_id(),
86	)
87}
88
89/// Provides the JSON representation of predefined genesis config for given `id`.
90pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
91	let patch = match id.as_ref() {
92		sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(),
93		sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET => local_config_genesis(),
94		_ => return None,
95	};
96	Some(
97		serde_json::to_string(&patch)
98			.expect("serialization to json is expected to work. qed.")
99			.into_bytes(),
100	)
101}
102
103/// List of supported presets.
104pub fn preset_names() -> Vec<PresetId> {
105	vec![
106		PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET),
107		PresetId::from(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET),
108	]
109}