#![allow(missing_docs)]
use cumulus_client_service::ParachainHostFunctions;
use cumulus_primitives_core::ParaId;
use cumulus_test_runtime::AccountId;
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup, GenesisConfigBuilderRuntimeCaller};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use serde_json::json;
pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
#[serde(deny_unknown_fields)]
pub struct Extensions {
pub para_id: u32,
}
impl Extensions {
pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
sc_chain_spec::get_extension(chain_spec.extensions())
}
}
pub fn get_chain_spec_with_extra_endowed(
id: Option<ParaId>,
extra_endowed_accounts: Vec<AccountId>,
code: &[u8],
) -> ChainSpec {
let runtime_caller = GenesisConfigBuilderRuntimeCaller::<ParachainHostFunctions>::new(code);
let mut development_preset = runtime_caller
.get_named_preset(Some(&sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET.to_string()))
.expect("development preset is available on test runtime; qed");
let existing_balances = development_preset
.get("balances")
.and_then(|b| b.get("balances"))
.and_then(|b| b.as_array())
.cloned()
.unwrap_or_default();
let mut all_balances = existing_balances;
all_balances.extend(extra_endowed_accounts.into_iter().map(|a| json!([a, 1u64 << 60])));
let mut patch_json = json!({
"balances": {
"balances": all_balances,
}
});
if let Some(id) = id {
sc_chain_spec::json_merge(
&mut patch_json,
json!({
"parachainInfo": {
"parachainId": id,
},
}),
);
};
sc_chain_spec::json_merge(&mut development_preset, patch_json.into());
ChainSpec::builder(
code,
Extensions { para_id: id.unwrap_or(cumulus_test_runtime::PARACHAIN_ID.into()).into() },
)
.with_name("Local Testnet")
.with_id(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET)
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(development_preset)
.build()
}
pub fn get_chain_spec(id: Option<ParaId>) -> ChainSpec {
get_chain_spec_with_extra_endowed(
id,
Default::default(),
cumulus_test_runtime::WASM_BINARY.expect("WASM binary was not built, please build it!"),
)
}
pub fn get_elastic_scaling_chain_spec(id: Option<ParaId>) -> ChainSpec {
get_chain_spec_with_extra_endowed(
id,
Default::default(),
cumulus_test_runtime::elastic_scaling::WASM_BINARY
.expect("WASM binary was not built, please build it!"),
)
}
pub fn get_elastic_scaling_mvp_chain_spec(id: Option<ParaId>) -> ChainSpec {
get_chain_spec_with_extra_endowed(
id,
Default::default(),
cumulus_test_runtime::elastic_scaling_mvp::WASM_BINARY
.expect("WASM binary was not built, please build it!"),
)
}