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

Introduce chain spec generator #78

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changelog for the runtimes governed by the Polkadot Fellowship.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

- Introduce chain spec generator ([polkadot-fellows/runtimes#78](https://github.com/polkadot-fellows/runtimes/pull/78))

## [1.0.0] 22.10.2023

### Changed
Expand Down
97 changes: 91 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "GPL-3.0-only" # TODO <https://github.com/polkadot-fellows/runtimes/is
resolver = "2"

members = [
"chain-spec-generator",
"relay/kusama",
"relay/kusama/constants",
"relay/polkadot",
Expand Down
35 changes: 35 additions & 0 deletions chain-spec-generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "chain-spec-generator"
version.workspace = true
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
clap = { version = "4.4.4", features = [ "derive" ] }
hex-literal = "0.4.1"
serde_json = "1.0.107"

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" }

sc-chain-spec = "22.0.0"
polkadot-runtime-parachains = "2.0.0"
polkadot-primitives = "2.0.0"
sp-consensus-babe = "0.27.0"
sp-authority-discovery = "21.0.0"
sp-core = "23.0.0"
pallet-staking = "23.0.0"
sc-consensus-grandpa = "0.14.0"
pallet-im-online = "22.0.0"
sp-runtime = "26.0.0"
sp-consensus-beefy = "8.0.0"

[features]
runtime-benchmarks = [
"kusama-runtime/runtime-benchmarks",
"polkadot-runtime/runtime-benchmarks",
]
54 changes: 54 additions & 0 deletions chain-spec-generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use clap::Parser;
use sc_chain_spec::ChainSpec;
use std::collections::HashMap;

mod relay_chain_specs;

#[derive(Parser)]
struct Cli {
/// The chain spec to generate.
chain: String,

/// Generate the chain spec as raw?
#[arg(long)]
raw: bool,
}

fn main() -> Result<(), String> {
let cli = Cli::parse();

let supported_chains =
HashMap::<_, Box<dyn Fn() -> Result<Box<dyn ChainSpec>, String>>>::from([
(
"polkadot-dev",
Box::new(|| relay_chain_specs::polkadot_development_config()) as Box<_>,
),
(
"polkadot-local",
Box::new(|| relay_chain_specs::polkadot_local_testnet_config()) as Box<_>,
),
("kusama-dev", Box::new(|| relay_chain_specs::kusama_development_config()) as Box<_>),
(
"kusama-local",
Box::new(|| relay_chain_specs::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 {
if cli.chain.ends_with(".json") {
let chain_spec = relay_chain_specs::from_json_file(&cli.chain)?.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"))
}
}
}
Loading