From 8d81834db77d0cdb68daa0faef029cfc2a6d7c04 Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Thu, 26 Dec 2024 18:20:39 -0300 Subject: [PATCH] feat(xtask): spec generating tool --- Cargo.lock | 1 + runtime/src/configs/eth.rs | 2 +- xtask/Cargo.toml | 1 + xtask/src/flags.rs | 28 ++++++++++++++++ xtask/src/main.rs | 68 +++++++++++++++++++++++++++++++++++++- xtask/src/run.rs | 5 +-- 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1222857..c48cc37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18521,6 +18521,7 @@ name = "xtask" version = "0.1.0" dependencies = [ "polkadot-sdk", + "serde_json", "tempfile", "xflags", ] diff --git a/runtime/src/configs/eth.rs b/runtime/src/configs/eth.rs index 3617d6d..9a14d55 100644 --- a/runtime/src/configs/eth.rs +++ b/runtime/src/configs/eth.rs @@ -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 diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index f02fc42..d2a38ee 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -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" diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index 369afc9..ccbf71f 100644 --- a/xtask/src/flags.rs +++ b/xtask/src/flags.rs @@ -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 + } } } @@ -58,6 +75,7 @@ pub struct Xtask { #[derive(Debug)] pub enum XtaskCmd { Run(Run), + GenerateSpec(GenerateSpec), } #[derive(Debug)] @@ -86,6 +104,16 @@ pub struct Local { pub account_suri: Option, } +#[derive(Debug)] +pub struct GenerateSpec { + pub base_chain_spec: Option, + pub out: PathBuf, + pub gran: Vec, + pub aura: Vec, + pub balance: Vec, + pub sudo: Option, +} + impl Xtask { #[allow(dead_code)] pub fn from_env_or_exit() -> Self { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index a85063f..2540e0a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, net::IpAddr}; +use std::{borrow::Cow, net::IpAddr, path::Path}; use polkadot_sdk::sp_keyring; @@ -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: ="); + 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 "); + } } } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index abbe122..9dd6477 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -75,7 +75,7 @@ pub(super) fn run(mut r: flags::Run) { } #[allow(dead_code)] -mod ops { +pub mod ops { use super::*; use std::{ ffi::OsStr, @@ -83,9 +83,10 @@ mod ops { 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