referrerpolicy=no-referrer-when-downgrade

cumulus_test_service/
chain_spec.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Cumulus is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17#![allow(missing_docs)]
18
19use cumulus_client_service::ParachainHostFunctions;
20use cumulus_primitives_core::ParaId;
21use cumulus_test_runtime::AccountId;
22use sc_chain_spec::GenesisConfigBuilderRuntimeCaller;
23use sc_service::{ChainType, GenericChainSpec};
24use serde_json::json;
25
26/// Get the chain spec for a specific parachain ID.
27/// The given accounts are initialized with funds in addition
28/// to the default known accounts.
29pub fn get_chain_spec_with_extra_endowed(
30	id: Option<ParaId>,
31	extra_endowed_accounts: Vec<AccountId>,
32	code: &[u8],
33) -> GenericChainSpec {
34	let runtime_caller = GenesisConfigBuilderRuntimeCaller::<ParachainHostFunctions>::new(code);
35	let mut development_preset = runtime_caller
36		.get_named_preset(Some(&sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET.to_string()))
37		.expect("development preset is available on test runtime; qed");
38
39	// Extract existing balances
40	let existing_balances = development_preset
41		.get("balances")
42		.and_then(|b| b.get("balances"))
43		.and_then(|b| b.as_array())
44		.cloned()
45		.unwrap_or_default();
46
47	// Create new balances by combining existing and extra accounts
48	let mut all_balances = existing_balances;
49	all_balances.extend(extra_endowed_accounts.into_iter().map(|a| json!([a, 1u64 << 60])));
50
51	let mut patch_json = json!({
52		"balances": {
53			"balances": all_balances,
54		},
55	});
56
57	if let Some(id) = id {
58		// Merge parachain ID if given, otherwise use the one from the preset.
59		sc_chain_spec::json_merge(
60			&mut patch_json,
61			json!({
62				"parachainInfo": {
63					"parachainId": id,
64				},
65
66			}),
67		);
68	};
69
70	sc_chain_spec::json_merge(&mut development_preset, patch_json.into());
71
72	GenericChainSpec::builder(code, None)
73		.with_name("Local Testnet")
74		.with_id(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET)
75		.with_chain_type(ChainType::Local)
76		.with_genesis_config_patch(development_preset)
77		.build()
78}
79
80/// Get the chain spec for a specific parachain ID.
81pub fn get_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
82	get_chain_spec_with_extra_endowed(
83		id,
84		Default::default(),
85		cumulus_test_runtime::WASM_BINARY.expect("WASM binary was not built, please build it!"),
86	)
87}
88
89/// Get the chain spec for a specific parachain ID.
90pub fn get_elastic_scaling_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
91	get_chain_spec_with_extra_endowed(
92		id,
93		Default::default(),
94		cumulus_test_runtime::elastic_scaling::WASM_BINARY
95			.expect("WASM binary was not built, please build it!"),
96	)
97}
98
99pub fn get_relay_parent_offset_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
100	get_chain_spec_with_extra_endowed(
101		id,
102		Default::default(),
103		cumulus_test_runtime::relay_parent_offset::WASM_BINARY
104			.expect("WASM binary was not built, please build it!"),
105	)
106}
107
108/// Get the chain spec for a specific parachain ID.
109pub fn get_elastic_scaling_500ms_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
110	get_chain_spec_with_extra_endowed(
111		id,
112		Default::default(),
113		cumulus_test_runtime::elastic_scaling_500ms::WASM_BINARY
114			.expect("WASM binary was not built, please build it!"),
115	)
116}
117
118/// Get the chain spec for a specific parachain ID.
119pub fn get_elastic_scaling_mvp_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
120	get_chain_spec_with_extra_endowed(
121		id,
122		Default::default(),
123		cumulus_test_runtime::elastic_scaling_mvp::WASM_BINARY
124			.expect("WASM binary was not built, please build it!"),
125	)
126}
127
128pub fn get_elastic_scaling_multi_block_slot_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
129	get_chain_spec_with_extra_endowed(
130		id,
131		Default::default(),
132		cumulus_test_runtime::elastic_scaling_multi_block_slot::WASM_BINARY
133			.expect("WASM binary was not built, please build it!"),
134	)
135}
136
137pub fn get_sync_backing_chain_spec(id: Option<ParaId>) -> GenericChainSpec {
138	get_chain_spec_with_extra_endowed(
139		id,
140		Default::default(),
141		cumulus_test_runtime::sync_backing::WASM_BINARY
142			.expect("WASM binary was not built, please build it!"),
143	)
144}