Skip to content

Commit

Permalink
feat(xtask): spec generating tool
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Dec 26, 2024
1 parent 71fabb2 commit 8d81834
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
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.

2 changes: 1 addition & 1 deletion runtime/src/configs/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
/// Maximum weight per block
pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX)
.saturating_mul(2)
.set_proof_size(MAX_POV_SIZE as u64);
.set_proof_size(MAX_POV_SIZE);

parameter_types! {
pub BlockGasLimit: U256
Expand Down
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
xflags = "0.3.2"
polkadot-sdk = { workspace = true, features = ["std", "sp-keyring"] }
tempfile = "3.14.0"
serde_json = "1"
28 changes: 28 additions & 0 deletions xtask/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ xflags::xflags! {
optional --account-suri account_suri: String
}
}

/// Generates a new spec file for the test net.
cmd generate-spec {
/// The base chain spec to use. Default dev will be used when empty.
optional -c, --base-chain-spec base_chain_spec: PathBuf

/// Output file for the chain spec.
required -o, --out output: PathBuf

repeated --gran gran_keys: String

repeated --aura gran_keys: String

repeated --balance balances: String

optional --sudo sudo_key: String
}
}
}

Expand All @@ -58,6 +75,7 @@ pub struct Xtask {
#[derive(Debug)]
pub enum XtaskCmd {
Run(Run),
GenerateSpec(GenerateSpec),
}

#[derive(Debug)]
Expand Down Expand Up @@ -86,6 +104,16 @@ pub struct Local {
pub account_suri: Option<String>,
}

#[derive(Debug)]
pub struct GenerateSpec {
pub base_chain_spec: Option<PathBuf>,
pub out: PathBuf,
pub gran: Vec<String>,
pub aura: Vec<String>,
pub balance: Vec<String>,
pub sudo: Option<String>,
}

impl Xtask {
#[allow(dead_code)]
pub fn from_env_or_exit() -> Self {
Expand Down
68 changes: 67 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, net::IpAddr};
use std::{borrow::Cow, net::IpAddr, path::Path};

use polkadot_sdk::sp_keyring;

Expand All @@ -9,6 +9,72 @@ fn main() {
let cmd = flags::Xtask::from_env_or_exit();
match cmd.subcommand {
flags::XtaskCmd::Run(run) => run::run(run),
flags::XtaskCmd::GenerateSpec(cmd) => {
let chain_spec = cmd
.base_chain_spec
.unwrap_or_else(|| Path::new("dev").to_path_buf());

let out = torus_node!("build-spec", "--chain", chain_spec)
.output()
.expect("failed to run torus node");

let out = if !cmd.aura.is_empty()
|| !cmd.gran.is_empty()
|| !cmd.balance.is_empty()
|| cmd.sudo.is_some()
{
use serde_json::{Number, Value};

let mut json: Value =
serde_json::from_slice(&out.stdout).expect("failed to parse spec file");

let patch = &mut json["genesis"]["runtimeGenesis"]["patch"];

if !cmd.aura.is_empty() {
let aura_keys = &mut patch["aura"]["authorities"]
.as_array_mut()
.expect("missing aura keys");
aura_keys.clear();

for aura in cmd.aura {
aura_keys.push(aura.into());
}
}

if !cmd.gran.is_empty() {
let gran_keys = patch["grandpa"]["authorities"]
.as_array_mut()
.expect("missing grandpa keys");
gran_keys.clear();

for gran in cmd.gran {
gran_keys.push([Value::from(gran), 1i32.into()].into());
}
}

for balance in cmd.balance {
let (account, amount) = balance
.split_once('=')
.expect("malformed balance entry, format: <account>=<amount>");
let amount: u128 = amount.parse().expect("balance amount must be a number");

patch["balances"]["balances"]
.as_array_mut()
.expect("missing grandpa keys")
.push([Value::from(account), Number::from_u128(amount).into()].into());
}

if let Some(sudo) = cmd.sudo {
patch["sudo"]["key"] = sudo.into();
}

serde_json::to_vec(&json).expect("failed to generate spec file")
} else {
out.stdout
};

std::fs::write(cmd.out, out).expect("failed to write resulting ");
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions xtask/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,18 @@ pub(super) fn run(mut r: flags::Run) {
}

#[allow(dead_code)]
mod ops {
pub mod ops {
use super::*;
use std::{
ffi::OsStr,
io::Write,
process::{Command, Stdio},
};

#[macro_export]
macro_rules! torus_node {
($($arg:expr),*) => {{
let mut cmd = Command::new("cargo");
let mut cmd = std::process::Command::new("cargo");
cmd.args(["run", "--release", "--package", "torus-node", "--"]);
$(cmd.arg($arg);)*
cmd
Expand Down

0 comments on commit 8d81834

Please sign in to comment.