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

Add parachain related parameters to chain-spec-builder #4889

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2ddd617
added extension to generic chain spec
CrackTheCode016 Jun 26, 2024
30be161
remove accidental chain_spec
CrackTheCode016 Jun 26, 2024
d70a1b7
fmt
CrackTheCode016 Jun 26, 2024
4e4b6df
Add fix for swallowing fields and optional parachain flag
CrackTheCode016 Jul 1, 2024
b2bd2dd
Add suggested change + initial PRDoc
CrackTheCode016 Jul 6, 2024
c1b80da
fix test with curr code
CrackTheCode016 Jul 6, 2024
6b960a4
fmt
CrackTheCode016 Jul 6, 2024
9ecbde4
fix doc test
CrackTheCode016 Jul 8, 2024
80c7290
Merge branch 'master' into bader-chain-spec-ext
CrackTheCode016 Jul 8, 2024
49babf2
Merge branch 'master' into bader-chain-spec-ext
CrackTheCode016 Aug 12, 2024
8cafcdf
remove opinionation + add parachain spec builder
CrackTheCode016 Aug 14, 2024
3a0ea7a
re-fmt
CrackTheCode016 Aug 20, 2024
2f52a65
Update substrate/bin/utils/chain-spec-builder/src/lib.rs
CrackTheCode016 Aug 21, 2024
7695df1
Update substrate/bin/utils/chain-spec-builder/src/lib.rs
CrackTheCode016 Aug 21, 2024
217056d
testing access
michalkucharczyk Sep 2, 2024
8f6c519
little re-org, tests added
michalkucharczyk Sep 3, 2024
f07a7f3
swallowing fields avoided, more tests added
michalkucharczyk Sep 3, 2024
cb37095
Merge remote-tracking branch 'origin/master' into bader-chain-spec-ext
michalkucharczyk Sep 3, 2024
5088ba7
license added to test
michalkucharczyk Sep 3, 2024
ce556f1
fmt
michalkucharczyk Sep 3, 2024
7c18843
fixes
michalkucharczyk Sep 3, 2024
929941f
one more fix
michalkucharczyk Sep 3, 2024
b9bcf93
Update substrate/bin/utils/chain-spec-builder/src/lib.rs
michalkucharczyk Sep 3, 2024
eee8a24
Update substrate/bin/utils/chain-spec-builder/src/lib.rs
michalkucharczyk Sep 3, 2024
e65f702
Merge branch 'master' into bader-chain-spec-ext
michalkucharczyk Sep 3, 2024
3c440ee
hardcoded runtime path removed from tests
michalkucharczyk Sep 4, 2024
d8d6e76
doc fix
michalkucharczyk Sep 4, 2024
7c39ace
missing files + naming aligned
michalkucharczyk Sep 4, 2024
fad5745
fmt
michalkucharczyk Sep 4, 2024
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
1 change: 1 addition & 0 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 substrate/bin/utils/chain-spec-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ clap = { features = ["derive"], workspace = true }
log = { workspace = true, default-features = true }
sc-chain-spec = { features = ["clap"], workspace = true, default-features = true }
serde_json = { workspace = true, default-features = true }
serde = { workspace = true, default-features = true }
sp-tracing = { workspace = true, default-features = true }
25 changes: 13 additions & 12 deletions substrate/bin/utils/chain-spec-builder/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use sc_chain_spec::{
GenesisConfigBuilderRuntimeCaller,
};
use staging_chain_spec_builder as chain_spec_builder;
use std::fs;
use std::{fs, path::Path};

type ChainSpec = GenericChainSpec<(), ()>;
type ChainSpec = GenericChainSpec<()>;

//avoid error message escaping
fn main() {
Expand All @@ -38,6 +38,14 @@ fn main() {
}
}

/// Extract any chain spec and convert it to JSON
fn extract_chain_spec_json(input_chain_spec: &Path) -> Result<serde_json::Value, String> {
let chain_spec = &fs::read(input_chain_spec)
.map_err(|e| format!("Provided chain spec could not be read: {e}"))?;

serde_json::from_slice(&chain_spec).map_err(|e| format!("Conversion to json failed: {e}"))
}

fn inner_main() -> Result<(), String> {
sp_tracing::try_init_simple();

Expand All @@ -53,11 +61,8 @@ fn inner_main() -> Result<(), String> {
ref input_chain_spec,
ref runtime_wasm_path,
}) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;
let mut chain_spec_json = extract_chain_spec_json(input_chain_spec.as_path())?;

let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
update_code_in_json_chain_spec(
&mut chain_spec_json,
&fs::read(runtime_wasm_path.as_path())
Expand All @@ -73,11 +78,7 @@ fn inner_main() -> Result<(), String> {
ref runtime_wasm_path,
block_height,
}) => {
let chain_spec = ChainSpec::from_json_file(input_chain_spec.clone())?;

let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
let mut chain_spec_json = extract_chain_spec_json(input_chain_spec.as_path())?;

set_code_substitute_in_json_chain_spec(
&mut chain_spec_json,
Expand Down Expand Up @@ -135,6 +136,6 @@ fn inner_main() -> Result<(), String> {
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
println!("{preset}");
},
};
}
Ok(())
}
43 changes: 39 additions & 4 deletions substrate/bin/utils/chain-spec-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@
use std::{fs, path::PathBuf};

use clap::{Parser, Subcommand};
use sc_chain_spec::{ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
use sc_chain_spec::{
ChainSpecExtension, ChainSpecGroup, ChainType, GenericChainSpec,
GenesisConfigBuilderRuntimeCaller,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// A utility to easily create a chain spec definition.
#[derive(Debug, Parser)]
#[command(rename_all = "kebab-case", version, about)]
Expand Down Expand Up @@ -158,6 +161,15 @@ pub struct CreateCmd {
/// The chain type.
#[arg(value_enum, short = 't', default_value = "live")]
chain_type: ChainType,
/// If you're generating a config for a parachain
#[arg(long, value_enum, default_value = "false")]
parachain: bool,
/// The para ID for your chain.
#[arg(value_enum, short = 'p', default_value = "100")]
para_id: u32,
/// The relay chain you wish to connect to.
#[arg(value_enum, short = 'c', default_value = "polkadot")]
relay_chain: String,
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
/// The path to runtime wasm blob.
#[arg(long, short)]
runtime_wasm_path: PathBuf,
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -279,17 +291,40 @@ pub struct VerifyCmd {
pub input_chain_spec: PathBuf,
}

/// The extensions for the [`ChainSpec`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
#[serde(deny_unknown_fields)]
pub struct Extensions {
/// The relay chain of the Parachain.
pub relay_chain: Option<String>,
/// The id of the Parachain.
pub para_id: Option<u32>,
}

/// Processes `CreateCmd` and returns JSON version of `ChainSpec`.
pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String> {
let code = fs::read(cmd.runtime_wasm_path.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;

let chain_type = &cmd.chain_type;
let relay_chain = &cmd.relay_chain;
let builder = match cmd.parachain {
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
true => GenericChainSpec::<Extensions>::builder(
&code[..],
Extensions { relay_chain: Some(relay_chain.to_string()), para_id: Some(cmd.para_id) },
)
.with_name(&cmd.chain_name[..])
.with_id(&cmd.chain_id[..])
.with_chain_type(chain_type.clone()),

let builder = GenericChainSpec::<()>::builder(&code[..], Default::default())
false => GenericChainSpec::<Extensions>::builder(
&code[..],
Extensions { relay_chain: None, para_id: None },
)
.with_name(&cmd.chain_name[..])
.with_id(&cmd.chain_id[..])
.with_chain_type(chain_type.clone());
.with_chain_type(chain_type.clone()),
};

let builder = match cmd.action {
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) =>
Expand Down
Loading