use polkadot_cli::{Error, Result};
use polkadot_node_primitives::CollationGenerationConfig;
use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
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_undying_collator::Collator;
mod cli;
use cli::Cli;
fn main() -> Result<()> {
let cli = Cli::from_args();
match cli.subcommand {
Some(cli::Subcommand::ExportGenesisState(params)) => {
let collator = Collator::new(params.pov_size, params.pvf_complexity);
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)) => {
let collator = Collator::default();
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(())
},
None => {
let runner = cli.create_runner(&cli.run.base).map_err(|e| {
SubstrateCliError::Application(
Box::new(e) as Box<(dyn 'static + Send + Sync + std::error::Error)>
)
})?;
runner.run_node_until_exit(|config| async move {
let collator = Collator::new(cli.run.pov_size, cli.run.pvf_complexity);
let full_node = polkadot_service::build_full(
config,
polkadot_service::NewFullParams {
is_parachain_node: polkadot_service::IsParachainNode::Collator(
collator.collator_key(),
),
enable_beefy: false,
force_authoring_backoff: false,
telemetry_worker_handle: None,
node_version: None,
secure_validator_mode: false,
workers_path: None,
workers_names: None,
overseer_gen: polkadot_service::CollatorOverseerGen,
overseer_message_channel_capacity_override: None,
malus_finality_delay: None,
hwbench: None,
execute_workers_max_num: None,
prepare_workers_hard_max_num: None,
prepare_workers_soft_max_num: None,
enable_approval_voting_parallel: false,
},
)
.map_err(|e| e.to_string())?;
let mut overseer_handle = full_node
.overseer_handle
.expect("Overseer handle should be initialized for collators");
let genesis_head_hex =
format!("0x{:?}", HexDisplay::from(&collator.genesis_head()));
let validation_code_hex =
format!("0x{:?}", HexDisplay::from(&collator.validation_code()));
let para_id = ParaId::from(cli.run.parachain_id);
log::info!("Running `Undying` collator for parachain id: {}", para_id);
log::info!("Genesis state: {}", genesis_head_hex);
log::info!("Validation code: {}", validation_code_hex);
let config = CollationGenerationConfig {
key: collator.collator_key(),
collator: Some(
collator.create_collation_function(full_node.task_manager.spawn_handle()),
),
para_id,
};
overseer_handle
.send_msg(CollationGenerationMessage::Initialize(config), "Collator")
.await;
overseer_handle
.send_msg(CollatorProtocolMessage::CollateOn(para_id), "Collator")
.await;
Ok(full_node.task_manager)
})
},
}?;
Ok(())
}