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(wasm_binary: Option<&[u8]>, id: Option<ParaId>) -> GenericChainSpec {
82	get_chain_spec_with_extra_endowed(
83		id,
84		Default::default(),
85		wasm_binary.expect("WASM binary was not built, please build it!"),
86	)
87}