polkadot_parachain/chain_spec/
penpal.rs1use cumulus_primitives_core::ParaId;
18use hex_literal::hex;
19use parachains_common::AccountId;
20use polkadot_omni_node_lib::chain_spec::{Extensions, GenericChainSpec};
21use sc_service::ChainType;
22
23pub fn get_penpal_chain_spec(id: ParaId, relay_chain: &str) -> GenericChainSpec {
24 let mut properties = sc_chain_spec::Properties::new();
26 properties.insert("tokenSymbol".into(), "UNIT".into());
27 properties.insert("tokenDecimals".into(), 12u32.into());
28 properties.insert("ss58Format".into(), 42u32.into());
29
30 GenericChainSpec::builder(
31 penpal_runtime::WASM_BINARY.expect("WASM binary was not built, please build it!"),
32 Extensions::new_with_relay_chain(relay_chain.into()),
33 )
34 .with_name("Penpal Parachain")
35 .with_id(&format!("penpal-{}", relay_chain.replace("-local", "")))
36 .with_chain_type(ChainType::Local)
37 .with_genesis_config_preset_name(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET)
38 .with_genesis_config_patch(serde_json::json!({
39 "parachainInfo": {
40 "parachainId": id,
41 },
42 }))
43 .build()
44}
45
46pub fn staging_penpal_local_config() -> GenericChainSpec {
47 GenericChainSpec::builder(
48 penpal_runtime::WASM_BINARY.expect("WASM binary was not built, please build it!"),
49 Extensions::new_with_relay_chain("rococo-local".into()),
50 )
51 .with_name("Staging Rococo Penpal Local")
52 .with_id("staging_testnet")
53 .with_chain_type(ChainType::Live)
54 .with_genesis_config_preset_name(sp_genesis_builder::LOCAL_TESTNET_RUNTIME_PRESET)
55 .with_genesis_config_patch(testnet_genesis_patch(
56 hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into(),
57 vec![hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into()],
58 1000.into(),
59 ))
60 .build()
61}
62
63pub(crate) fn testnet_genesis_patch(
64 root_key: AccountId,
65 endowed_accounts: Vec<AccountId>,
66 id: ParaId,
67) -> serde_json::Value {
68 serde_json::json!({
69 "balances": {
70 "balances": endowed_accounts.iter().cloned().map(|k| (k, 1u64 << 60)).collect::<Vec<_>>(),
71 },
72 "sudo": { "key": Some(root_key) },
73 "parachainInfo": {
74 "parachainId": id,
75 },
76 })
77}
78
79#[cfg(test)]
80mod test {
81 use super::*;
82 use penpal_runtime::BuildStorage;
83
84 #[test]
85 fn staging_penpal_local_config_works() {
86 let chain_spec = Box::new(staging_penpal_local_config());
87 chain_spec
88 .build_storage()
89 .expect("build_storage from staging chain-spec (default) config should works.");
90 }
91
92 #[test]
93 fn penpal_chain_spec_works() {
94 let chain_spec = Box::new(get_penpal_chain_spec(1002.into(), "rococo"));
95 chain_spec
96 .build_storage()
97 .expect("build_storage from staging chain-spec (default) config should works.");
98 }
99
100 #[test]
101 fn staging_penpal_invalid_config_err() {
102 use parachains_common::AuraId;
103 use serde_json::{json, Value};
104 use sp_core::crypto::UncheckedInto;
105
106 let aura_auth: AuraId =
107 hex!["aad9fa2249f87a210a0f93400b7f90e47b810c6d65caa0ca3f5af982904c2a33"]
108 .unchecked_into();
109
110 let chain_spec = Box::new(staging_penpal_local_config());
111 let mut chain_spec_json: Value = serde_json::from_str(
112 &chain_spec
113 .as_json(false)
114 .expect("serialization to json is expected to work. qed."),
115 )
116 .expect("serialization to json Value is expected to work. qed.");
117 chain_spec_json["genesis"]["runtimeGenesis"]["patch"]["aura"] =
118 json!({"authorities" : vec![ aura_auth ] });
119
120 let chain_spec_invalid_config =
121 GenericChainSpec::from_json_bytes(chain_spec_json.to_string().as_bytes().to_vec())
122 .expect("parse json content into a ChainSpec should works. qed");
123 let result = chain_spec_invalid_config.build_storage();
124 assert!(result.is_err());
125 }
126}