Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend chain spec generator for System Parachains #81

9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions chain-spec-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ license.workspace = true
clap = { version = "4.4.4", features = [ "derive" ] }
hex-literal = "0.4.1"
serde_json = "1.0.107"
serde = { version = "1.0.188", features = ["derive"] }

polkadot-runtime = { path = "../relay/polkadot" }
polkadot-runtime-constants = { path = "../relay/polkadot/constants" }
kusama-runtime = { package = "staging-kusama-runtime", path = "../relay/kusama" }
kusama-runtime-constants = { path = "../relay/kusama/constants" }

asset-hub-polkadot-runtime = { path = "../system-parachains/asset-hubs/asset-hub-polkadot" }
asset-hub-kusama-runtime = { path = "../system-parachains/asset-hubs/asset-hub-kusama" }
collectives-polkadot-runtime = { path = "../system-parachains/collectives/collectives-polkadot" }
bridge-hub-polkadot-runtime = { path = "../system-parachains/bridge-hubs/bridge-hub-polkadot" }
bridge-hub-kusama-runtime = { path = "../system-parachains/bridge-hubs/bridge-hub-kusama" }

sc-chain-spec = "22.0.0"
polkadot-runtime-parachains = "2.0.0"
polkadot-primitives = "2.0.0"
Expand All @@ -27,3 +34,6 @@ sc-consensus-grandpa = "0.14.0"
pallet-im-online = "22.0.0"
sp-runtime = "26.0.0"
sp-consensus-beefy = "8.0.0"
xcm = { package = "staging-xcm", version = "2.0.1" }
parachains-common = { version = "2.0.0" }
cumulus-primitives-core = { version = "0.2.0" }
73 changes: 73 additions & 0 deletions chain-spec-generator/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::{
relay_chain_specs::{KusamaChainSpec, PolkadotChainSpec},
system_parachains_specs::{
AssetHubKusamaChainSpec, AssetHubPolkadotChainSpec, BridgeHubKusamaChainSpec,
BridgeHubPolkadotChainSpec, CollectivesPolkadotChainSpec,
},
ChainSpec,
};
use polkadot_primitives::{AccountId, AccountPublic};
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::IdentifyAccount;

pub fn testnet_accounts() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
]
}

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}

/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

#[derive(Debug, serde::Deserialize)]
struct EmptyChainSpecWithId {
id: String,
}

pub fn from_json_file(filepath: &str, supported: String) -> Result<Box<dyn ChainSpec>, String> {
let path = std::path::PathBuf::from(&filepath);
let file = std::fs::File::open(&filepath).expect("Failed to open file");
let reader = std::io::BufReader::new(file);
let chain_spec: EmptyChainSpecWithId = serde_json::from_reader(reader)
.expect("Failed to read 'json' file with ChainSpec configuration");
match &chain_spec.id {
x if x.starts_with("polkadot") | x.starts_with("dot") =>
Ok(Box::new(PolkadotChainSpec::from_json_file(path)?)),
x if x.starts_with("kusama") | x.starts_with("ksm") =>
Ok(Box::new(KusamaChainSpec::from_json_file(path)?)),
x if x.starts_with("asset-hub-polkadot") =>
Ok(Box::new(AssetHubPolkadotChainSpec::from_json_file(path)?)),
x if x.starts_with("asset-hub-kusama") =>
Ok(Box::new(AssetHubKusamaChainSpec::from_json_file(path)?)),
x if x.starts_with("collectives-polkadot") =>
Ok(Box::new(CollectivesPolkadotChainSpec::from_json_file(path)?)),
x if x.starts_with("bridge-hub-polkadot") =>
Ok(Box::new(BridgeHubPolkadotChainSpec::from_json_file(path)?)),
x if x.starts_with("bridge-hub-kusama") =>
Ok(Box::new(BridgeHubKusamaChainSpec::from_json_file(path)?)),
_ => Err(format!("Unknown chain 'id' in json file. Only supported: {supported}'")),
}
}
37 changes: 32 additions & 5 deletions chain-spec-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use clap::Parser;
use sc_chain_spec::ChainSpec;
use std::collections::HashMap;

mod common;
mod relay_chain_specs;
mod system_parachains_specs;

#[derive(Parser)]
struct Cli {
Expand Down Expand Up @@ -32,22 +34,47 @@ fn main() -> Result<(), String> {
"kusama-local",
Box::new(|| relay_chain_specs::kusama_local_testnet_config()) as Box<_>,
),
(
"asset-hub-kusama-local",
Box::new(|| system_parachains_specs::asset_hub_kusama_local_testnet_config())
as Box<_>,
),
(
"asset-hub-polkadot-local",
Box::new(|| system_parachains_specs::asset_hub_polkadot_local_testnet_config())
as Box<_>,
),
(
"collectives-polkadot-local",
Box::new(|| system_parachains_specs::collectives_polkadot_local_testnet_config())
as Box<_>,
),
(
"bridge-hub-polkadot-local",
Box::new(|| system_parachains_specs::bridge_hub_polkadot_local_testnet_config())
as Box<_>,
),
(
"bridge-hub-kusama-local",
Box::new(|| system_parachains_specs::bridge_hub_kusama_local_testnet_config())
as Box<_>,
),
]);

if let Some(function) = supported_chains.get(&*cli.chain) {
let chain_spec = (*function)()?.as_json(cli.raw)?;
print!("{chain_spec}");
Ok(())
} else {
let supported = supported_chains.keys().enumerate().fold(String::new(), |c, (n, k)| {
let extra = (n + 1 < supported_chains.len()).then(|| ", ").unwrap_or("");
format!("{c}{k}{extra}")
});
if cli.chain.ends_with(".json") {
let chain_spec = relay_chain_specs::from_json_file(&cli.chain)?.as_json(cli.raw)?;
let chain_spec = common::from_json_file(&cli.chain, supported)?.as_json(cli.raw)?;
print!("{chain_spec}");
Ok(())
} else {
let supported = supported_chains.keys().enumerate().fold(String::new(), |c, (n, k)| {
let extra = (n + 1 < supported_chains.len()).then(|| ", ").unwrap_or("");
format!("{c}{k}{extra}")
});
Err(format!("Unknown chain, only supported: {supported} or a json file"))
}
}
Expand Down
18 changes: 4 additions & 14 deletions chain-spec-generator/src/relay_chain_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ pub fn polkadot_development_config() -> Result<Box<dyn ChainSpec>, String> {
polkadot_runtime::WASM_BINARY.ok_or("Polkadot development wasm not available")?;

Ok(Box::new(PolkadotChainSpec::from_genesis(
"Development",
"Polakdot Development",
"polkadot_dev",
ChainType::Development,
move || polkadot_development_config_genesis(wasm_binary),
Expand All @@ -392,7 +392,7 @@ pub fn kusama_development_config() -> Result<Box<dyn ChainSpec>, String> {
let wasm_binary = kusama_runtime::WASM_BINARY.ok_or("Kusama development wasm not available")?;

Ok(Box::new(KusamaChainSpec::from_genesis(
"Development",
"Kusama Development",
"kusama_dev",
ChainType::Development,
move || kusama_development_config_genesis(wasm_binary),
Expand Down Expand Up @@ -423,8 +423,8 @@ pub fn polkadot_local_testnet_config() -> Result<Box<dyn ChainSpec>, String> {
polkadot_runtime::WASM_BINARY.ok_or("Polkadot development wasm not available")?;

Ok(Box::new(PolkadotChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
"Polkadot Local Testnet",
"polkadot_testnet",
ChainType::Local,
move || polkadot_local_testnet_genesis(wasm_binary),
vec![],
Expand Down Expand Up @@ -463,16 +463,6 @@ pub fn kusama_local_testnet_config() -> Result<Box<dyn ChainSpec>, String> {
)))
}

pub fn from_json_file(filepath: &str) -> Result<Box<dyn ChainSpec>, String> {
let path = std::path::PathBuf::from(&filepath);
let chain_spec = PolkadotChainSpec::from_json_file(path.clone())?;
match chain_spec.id() {
x if x.starts_with("kusama") | x.starts_with("ksm") =>
Ok(Box::new(KusamaChainSpec::from_json_file(path)?)),
_ => Ok(Box::new(chain_spec)),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading