use std::time::Duration;
use clap::Parser;
use miden_remote_prover::{COMPONENT, api::ProofType};
use proxy::StartProxy;
use tracing::instrument;
use update_workers::{AddWorkers, RemoveWorkers, UpdateWorkers};
use worker::StartWorker;
pub mod proxy;
pub mod update_workers;
pub mod worker;
pub(crate) const PROXY_HOST: &str = "0.0.0.0";
#[derive(Debug, Parser)]
pub(crate) struct ProxyConfig {
#[arg(long, default_value = "20ms", env = "MRP_AVAILABLE_WORKERS_POLLING_INTERVAL", value_parser = humantime::parse_duration)]
pub(crate) available_workers_polling_interval: Duration,
#[arg(long, default_value = "10s", env = "MRP_CONNECTION_TIMEOUT", value_parser = humantime::parse_duration)]
pub(crate) connection_timeout: Duration,
#[arg(long, default_value = "10s", env = "MRP_HEALTH_CHECK_INTERVAL", value_parser = humantime::parse_duration)]
pub(crate) health_check_interval: Duration,
#[arg(long, default_value = "10", env = "MRP_MAX_QUEUE_ITEMS")]
pub(crate) max_queue_items: usize,
#[arg(long, default_value = "5", env = "MRP_MAX_REQ_PER_SEC")]
pub(crate) max_req_per_sec: isize,
#[arg(long, default_value = "1", env = "MRP_MAX_RETRIES_PER_REQUEST")]
pub(crate) max_retries_per_request: usize,
#[command(flatten)]
pub(crate) metrics_config: MetricsConfig,
#[arg(long, default_value = "8082", env = "MRP_PORT")]
pub(crate) port: u16,
#[arg(long, default_value = "10s", env = "MRP_STATUS_UPDATE_INTERVAL", value_parser = humantime::parse_duration)]
pub(crate) status_update_interval: Duration,
#[arg(long, default_value = "100s", env = "MRP_TIMEOUT", value_parser = humantime::parse_duration)]
pub(crate) timeout: Duration,
#[arg(long, default_value = "8083", env = "MRP_CONTROL_PORT")]
pub(crate) control_port: u16,
#[arg(long, default_value = "transaction", env = "MRP_PROOF_TYPE")]
pub(crate) proof_type: ProofType,
#[arg(long, default_value = "8084", env = "MRP_STATUS_PORT")]
pub(crate) status_port: u16,
#[arg(long, default_value = "20s", env = "MRP_GRACE_PERIOD", value_parser = humantime::parse_duration)]
pub(crate) grace_period: std::time::Duration,
#[arg(long, default_value = "5s", env = "MRP_GRACEFUL_SHUTDOWN_TIMEOUT", value_parser = humantime::parse_duration)]
pub(crate) graceful_shutdown_timeout: std::time::Duration,
}
#[derive(Debug, Clone, clap::Parser)]
pub struct MetricsConfig {
#[arg(long, env = "MRP_METRICS_PORT")]
pub metrics_port: Option<u16>,
}
#[derive(Parser, Debug)]
#[command(
name = "miden-remote-prover",
about = "A stand-alone service for proving Miden transactions.",
version,
rename_all = "kebab-case"
)]
pub struct Cli {
#[command(subcommand)]
action: Command,
}
#[derive(Debug, Parser)]
pub enum Command {
StartWorker(StartWorker),
StartProxy(StartProxy),
AddWorkers(AddWorkers),
RemoveWorkers(RemoveWorkers),
}
impl Cli {
#[instrument(target = COMPONENT, name = "cli.execute", skip_all, ret(level = "info"), err)]
pub async fn execute(&self) -> Result<(), String> {
match &self.action {
Command::StartWorker(worker_init) => worker_init.execute().await,
Command::StartProxy(proxy_init) => proxy_init.execute().await,
Command::AddWorkers(update_workers) => {
let update_workers: UpdateWorkers = update_workers.clone().into();
update_workers.execute().await
},
Command::RemoveWorkers(update_workers) => {
let update_workers: UpdateWorkers = update_workers.clone().into();
update_workers.execute().await
},
}
}
}