Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat: run polygon edge from topos cli #220

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/topos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ assert_cmd = "2.0.6"
insta = { version = "1.21", features = ["json", "redactions"] }

[features]
default = ["tce", "sequencer", "network", "setup"]
default = ["tce", "sequencer", "network", "setup", "subnet"]
tce = ["topos-tce", "topos-tce-transport"]
sequencer = ["topos-sequencer"]
network = ["topos-certificate-spammer"]
setup = []
subnet = []

tce-direct = ["tce", "topos-tce-broadcast/direct"]
2 changes: 2 additions & 0 deletions crates/topos/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ pub(crate) mod network;
pub(crate) mod sequencer;
#[cfg(feature = "setup")]
pub(crate) mod setup;
#[cfg(feature = "subnet")]
pub(crate) mod subnet;
#[cfg(feature = "tce")]
pub(crate) mod tce;
1 change: 1 addition & 0 deletions crates/topos/src/components/network/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod spam;

pub(crate) use spam::Spam;

/// Topos CLI subcommand for network related functionalities (e.g., running the certificate spammer)
#[derive(Args, Debug)]
pub(crate) struct NetworkCommand {
#[clap(from_global)]
Expand Down
1 change: 1 addition & 0 deletions crates/topos/src/components/sequencer/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod run;

pub(crate) use run::Run;

/// Topos CLI subcommand for the Sequencer components
#[derive(Args, Debug)]
pub(crate) struct SequencerCommand {
#[clap(from_global)]
Expand Down
1 change: 1 addition & 0 deletions crates/topos/src/components/setup/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod subnet;

pub(crate) use subnet::Subnet;

/// Topos CLI subcommand for the setup of various Topos related components (e.g., installation of Polygon Edge binary)
#[derive(Args, Debug)]
pub(crate) struct SetupCommand {
#[clap(from_global)]
Expand Down
30 changes: 30 additions & 0 deletions crates/topos/src/components/subnet/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use clap::{Args, Subcommand};

mod subnet;

pub(crate) use subnet::Run;

/// Topos CLI subcommand for the Polygon Edge related functionalities
#[derive(Args, Debug)]
pub(crate) struct SubnetCommand {
atanmarko marked this conversation as resolved.
Show resolved Hide resolved
#[clap(from_global)]
pub(crate) verbose: u8,

#[clap(subcommand)]
pub(crate) subcommands: Option<SubnetCommands>,
}

#[derive(Subcommand, Debug)]
pub(crate) enum SubnetCommands {
Run(Run),
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_run() {
assert!(SubnetCommands::has_subcommand("run"));
}
}
17 changes: 17 additions & 0 deletions crates/topos/src/components/subnet/commands/subnet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::{ArgGroup, Args, Parser};
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[clap(about = "Run Polygon Edge", trailing_var_arg = true)]
pub struct Run {
/// Installation directory path for Polygon Edge binary.
/// If not provided, Polygon Edge binary will be expected in the current directory
#[arg(long, env = "TOPOS_POLYGON_EDGE_BIN_PATH", default_value = ".")]
pub path: PathBuf,

/// Polygon Edge command line arguments
#[clap(allow_hyphen_values = true)]
pub polygon_edge_arguments: Vec<String>,
}

impl Run {}
65 changes: 65 additions & 0 deletions crates/topos/src/components/subnet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use self::commands::{Run, SubnetCommand, SubnetCommands};
use clap::Parser;
use std::process::Stdio;
use tokio::{io::AsyncReadExt, io::BufReader, process, process::Command};
use tokio::{signal, spawn};
use tracing::{error, info};

use crate::tracing::setup_tracing;

pub(crate) mod commands;

pub(crate) async fn handle_command(
SubnetCommand {
subcommands,
verbose,
}: SubnetCommand,
) -> Result<(), Box<dyn std::error::Error>> {
match subcommands {
Some(SubnetCommands::Run(cmd)) => {
setup_tracing(verbose, None, None)?;

let binary_name =
"polygon-edge-".to_string() + std::env::consts::ARCH + "-" + std::env::consts::OS;
let polygon_edge_path = cmd.path.join(&binary_name);

info!(
"Running binary {} with arguments:{:?}",
polygon_edge_path.display(),
cmd.polygon_edge_arguments
);

spawn(async move {
// Run Polygon Edge command. Pass all parameters.
match Command::new(polygon_edge_path)
.args(cmd.polygon_edge_arguments)
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()
{
Ok(mut child) => {
if let Some(pid) = child.id() {
info!("Polygon Edge child process with pid {pid} successfully started");
}
if let Err(e) = child.wait().await {
info!("Polygon Edge child process finished with error: {e}");
}
std::process::exit(0);
}
Err(e) => {
error!("Error executing Polygon Edge: {e}");
std::process::exit(1);
}
}
});

signal::ctrl_c()
.await
.expect("failed to listen for signals");

Ok(())
}
None => Ok(()),
}
}
1 change: 1 addition & 0 deletions crates/topos/src/components/tce/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) struct NodeArgument {
pub(crate) node: String,
}

/// Topos CLI subcommand for the TCE related functionalities
#[derive(Args, Debug)]
pub(crate) struct TceCommand {
#[clap(from_global)]
Expand Down
2 changes: 2 additions & 0 deletions crates/topos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
options::ToposCommand::Network(cmd) => components::network::handle_command(cmd).await,
#[cfg(feature = "setup")]
options::ToposCommand::Setup(cmd) => components::setup::handle_command(cmd).await,
#[cfg(feature = "subnet")]
options::ToposCommand::Subnet(cmd) => components::subnet::handle_command(cmd).await,
}
}
5 changes: 5 additions & 0 deletions crates/topos/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use crate::components::network::commands::NetworkCommand;
#[cfg(feature = "setup")]
use crate::components::setup::commands::SetupCommand;

#[cfg(feature = "subnet")]
use crate::components::subnet::commands::SubnetCommand;

pub(crate) mod input_format;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -40,4 +43,6 @@ pub(crate) enum ToposCommand {
Network(NetworkCommand),
#[cfg(feature = "setup")]
Setup(SetupCommand),
#[cfg(feature = "subnet")]
Subnet(SubnetCommand),
}