Skip to content
Snippets Groups Projects
Unverified Commit c578318b authored by José Molina Colmenero's avatar José Molina Colmenero Committed by GitHub
Browse files

Add chain properties to chain-spec-builder (#7368)


This PR adds support for chain properties to `chain-spec-builder`. Now
properties can be specified as such:

```sh
$ chain-spec-builder create -r $RUNTIME_PATH --properties tokenSymbol=DUMMY,tokenDecimals=6,isEthereum=false
```

---------

Co-authored-by: default avatarBastian Köcher <git@kchr.de>
Co-authored-by: default avatarMichal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
parent 87ac3f2e
No related merge requests found
Pipeline #515524 waiting for manual action with stages
in 1 hour, 22 minutes, and 24 seconds
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
title: "Add chain properties to chain-spec-builder"
doc:
- audience: Node Dev
description: |
- Adds support for chain properties to chain-spec-builder.
crates:
- name: staging-chain-spec-builder
bump: minor
......@@ -83,6 +83,18 @@ pub struct CreateCmd {
/// errors will be reported.
#[arg(long, short = 'v')]
verify: bool,
/// Chain properties in `KEY=VALUE` format.
///
/// Multiple `KEY=VALUE` entries can be specified and separated by a comma.
///
/// Example: `--properties tokenSymbol=UNIT,tokenDecimals=12,ss58Format=42,isEthereum=false`
/// Or: `--properties tokenSymbol=UNIT --properties tokenDecimals=12 --properties ss58Format=42
/// --properties=isEthereum=false`
///
/// The first uses comma as separation and the second passes the argument multiple times. Both
/// styles can also be mixed.
#[arg(long, default_value = "tokenSymbol=UNIT,tokenDecimals=12")]
pub properties: Vec<String>,
#[command(subcommand)]
action: GenesisBuildAction,
......@@ -385,15 +397,44 @@ impl CreateCmd {
}
}
/// Processes `CreateCmd` and returns string represenataion of JSON version of `ChainSpec`.
/// Parses chain properties passed as a comma-separated KEY=VALUE pairs.
fn parse_properties(raw: &String, props: &mut sc_chain_spec::Properties) -> Result<(), String> {
for pair in raw.split(',') {
let mut iter = pair.splitn(2, '=');
let key = iter
.next()
.ok_or_else(|| format!("Invalid chain property key: {pair}"))?
.trim()
.to_owned();
let value_str = iter
.next()
.ok_or_else(|| format!("Invalid chain property value for key: {key}"))?
.trim();
// Try to parse as bool, number, or fallback to String
let value = match value_str.parse::<bool>() {
Ok(b) => Value::Bool(b),
Err(_) => match value_str.parse::<u32>() {
Ok(i) => Value::Number(i.into()),
Err(_) => Value::String(value_str.to_string()),
},
};
props.insert(key, value);
}
Ok(())
}
/// Processes `CreateCmd` and returns string representation of JSON version of `ChainSpec`.
pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String> {
let code = cmd.get_runtime_code()?;
let chain_type = &cmd.chain_type;
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "UNIT".into());
properties.insert("tokenDecimals".into(), 12.into());
for raw in &cmd.properties {
parse_properties(raw, &mut properties)?;
}
let builder = ChainSpec::builder(&code[..], Default::default())
.with_name(&cmd.chain_name[..])
......
{
"name": "Custom",
"id": "custom",
"chainType": "Live",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"properties": {
"tokenDecimals": 6,
"tokenSymbol": "TEST",
"ss58Prefix": 42,
"isEthereum": false
},
"codeSubstitutes": {},
"genesis": {
"runtimeGenesis": {
"code": "0x010203",
"config": {
"babe": {
"authorities": [],
"epochConfig": {
"allowed_slots": "PrimaryAndSecondaryVRFSlots",
"c": [
1,
4
]
}
},
"balances": {
"balances": [],
"devAccounts": null
},
"substrateTest": {
"authorities": []
},
"system": {}
}
}
}
}
......@@ -234,6 +234,29 @@ fn test_add_code_substitute() {
assert_output_eq_expected(true, SUFFIX, "tests/expected/add_code_substitute.json");
}
#[test]
fn test_create_with_properties() {
const SUFFIX: &str = "11";
let mut builder = get_builder(
SUFFIX,
vec![
"create",
"-r",
DUMMY_PATH,
"--properties",
"tokenSymbol=TEST,tokenDecimals=6",
"--properties",
"isEthereum=false",
"--properties",
"ss58Prefix=42",
"default",
],
);
builder.set_create_cmd_runtime_code(substrate_test_runtime::WASM_BINARY.unwrap().into());
builder.run().unwrap();
assert_output_eq_expected(true, SUFFIX, "tests/expected/create_with_properties.json");
}
#[docify::export_content]
fn cmd_create_default(runtime_path: &str) -> String {
bash!(
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment