From caa987d26d14bbdbfbc9be08a1da4c2b117fcaa3 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Sun, 21 Jan 2024 06:51:04 +0800 Subject: [PATCH] Adder Parachain: Accept output file argument to `export-genesis-*` subcommands (#2370) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR makes a small change the the adder parachain's CLI. It allows the user to specify an output file explicitly when generating the genesis wasm and head data. Now we no longer have to rely on redirecting the output to a file at the shell level. This change is nice because if you have any debugging lines enabled, shell redirection does not work. More to the point, this makes the adder parachain's CLI match the cumulus collator's CLI. And that will allow tools like Zombienet (that support both cumulus and the adder) to use the positional argument. cc @pepoviola --------- Co-authored-by: Joshy Orndorff Co-authored-by: Bastian Köcher --- .../test-parachains/adder/collator/src/cli.rs | 13 ++++++++-- .../adder/collator/src/main.rs | 26 ++++++++++++++++--- .../undying/collator/src/main.rs | 4 +-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/polkadot/parachain/test-parachains/adder/collator/src/cli.rs b/polkadot/parachain/test-parachains/adder/collator/src/cli.rs index f81e4cc0fff6..5e9b4f584d44 100644 --- a/polkadot/parachain/test-parachains/adder/collator/src/cli.rs +++ b/polkadot/parachain/test-parachains/adder/collator/src/cli.rs @@ -18,6 +18,7 @@ use clap::Parser; use sc_cli::SubstrateCli; +use std::path::PathBuf; /// Sub-commands supported by the collator. #[derive(Debug, Parser)] @@ -33,11 +34,19 @@ pub enum Subcommand { /// Command for exporting the genesis head data of the parachain #[derive(Debug, Parser)] -pub struct ExportGenesisHeadCommand {} +pub struct ExportGenesisHeadCommand { + /// Output file name or stdout if unspecified. + #[arg()] + pub output: Option, +} /// Command for exporting the genesis wasm file. #[derive(Debug, Parser)] -pub struct ExportGenesisWasmCommand {} +pub struct ExportGenesisWasmCommand { + /// Output file name or stdout if unspecified. + #[arg()] + pub output: Option, +} #[allow(missing_docs)] #[derive(Debug, Parser)] diff --git a/polkadot/parachain/test-parachains/adder/collator/src/main.rs b/polkadot/parachain/test-parachains/adder/collator/src/main.rs index 6ce93ef4ad14..5d9384d49561 100644 --- a/polkadot/parachain/test-parachains/adder/collator/src/main.rs +++ b/polkadot/parachain/test-parachains/adder/collator/src/main.rs @@ -22,6 +22,10 @@ use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProt use polkadot_primitives::Id as ParaId; use sc_cli::{Error as SubstrateCliError, SubstrateCli}; use sp_core::hexdisplay::HexDisplay; +use std::{ + fs, + io::{self, Write}, +}; use test_parachain_adder_collator::Collator; /// The parachain ID to collate for in case it wasn't set explicitly through CLI. @@ -34,15 +38,29 @@ fn main() -> Result<()> { let cli = Cli::from_args(); match cli.subcommand { - Some(cli::Subcommand::ExportGenesisState(_params)) => { + Some(cli::Subcommand::ExportGenesisState(params)) => { let collator = Collator::new(); - println!("0x{:?}", HexDisplay::from(&collator.genesis_head())); + let output_buf = + format!("0x{:?}", HexDisplay::from(&collator.genesis_head())).into_bytes(); + + if let Some(output) = params.output { + std::fs::write(output, output_buf)?; + } else { + std::io::stdout().write_all(&output_buf)?; + } Ok::<_, Error>(()) }, - Some(cli::Subcommand::ExportGenesisWasm(_params)) => { + Some(cli::Subcommand::ExportGenesisWasm(params)) => { let collator = Collator::new(); - println!("0x{:?}", HexDisplay::from(&collator.validation_code())); + let output_buf = + format!("0x{:?}", HexDisplay::from(&collator.validation_code())).into_bytes(); + + if let Some(output) = params.output { + fs::write(output, output_buf)?; + } else { + io::stdout().write_all(&output_buf)?; + } Ok(()) }, diff --git a/polkadot/parachain/test-parachains/undying/collator/src/main.rs b/polkadot/parachain/test-parachains/undying/collator/src/main.rs index 4a15cdd697c4..d6fb9a04ab30 100644 --- a/polkadot/parachain/test-parachains/undying/collator/src/main.rs +++ b/polkadot/parachain/test-parachains/undying/collator/src/main.rs @@ -54,9 +54,9 @@ fn main() -> Result<()> { Some(cli::Subcommand::ExportGenesisWasm(params)) => { // We pass some dummy values for `pov_size` and `pvf_complexity` as these don't // matter for `wasm` export. + let collator = Collator::default(); let output_buf = - format!("0x{:?}", HexDisplay::from(&Collator::default().validation_code())) - .into_bytes(); + format!("0x{:?}", HexDisplay::from(&collator.validation_code())).into_bytes(); if let Some(output) = params.output { fs::write(output, output_buf)?;