|
| 1 | +use clap::{Parser, Subcommand}; |
| 2 | +use eyre::WrapErr; |
| 3 | +use scroll_proving_sdk::db::{Db, PROVING_TASK_ID_KEY_PREFIX}; |
| 4 | +use scroll_proving_sdk::utils::init_color_eyre_hook; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +/// Tool to interact with the prover database. |
| 9 | +#[derive(Parser)] |
| 10 | +struct Cli { |
| 11 | + /// Path to the database |
| 12 | + #[clap(long, default_value = ".work/db")] |
| 13 | + db: PathBuf, |
| 14 | + |
| 15 | + #[command(subcommand)] |
| 16 | + commands: Commands, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Subcommand)] |
| 20 | +enum Commands { |
| 21 | + /// List all coordinator and prover task ID pairs |
| 22 | + List, |
| 23 | + /// Get the corresponding task ID |
| 24 | + Get { |
| 25 | + #[command(subcommand)] |
| 26 | + commands: GetCommands, |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Subcommand)] |
| 31 | +enum GetCommands { |
| 32 | + /// Get coordinator task ID by prover task ID |
| 33 | + Coordinator { task_id: String }, |
| 34 | + /// Get prover task ID by coordinator task ID |
| 35 | + Prover { task_id: String }, |
| 36 | +} |
| 37 | + |
| 38 | +fn main() -> eyre::Result<()> { |
| 39 | + init_color_eyre_hook(); |
| 40 | + |
| 41 | + let cli = Cli::parse(); |
| 42 | + let db = Db::new(&cli.db).context("open database")?; |
| 43 | + |
| 44 | + let mut c2p = HashMap::new(); |
| 45 | + let mut p2c = HashMap::new(); |
| 46 | + |
| 47 | + for result in db.inner().prefix_iterator(PROVING_TASK_ID_KEY_PREFIX) { |
| 48 | + let (proving_task_key_bytes, _) = result.context("iter entry")?; |
| 49 | + let public_key = |
| 50 | + std::str::from_utf8(&proving_task_key_bytes[PROVING_TASK_ID_KEY_PREFIX.len()..])?; |
| 51 | + |
| 52 | + let Some((coordinator_task, prover_task_id)) = db.get_task(public_key) else { |
| 53 | + continue; |
| 54 | + }; |
| 55 | + |
| 56 | + c2p.insert(coordinator_task.task_id.clone(), prover_task_id.clone()); |
| 57 | + p2c.insert(prover_task_id, coordinator_task.task_id); |
| 58 | + } |
| 59 | + |
| 60 | + match cli.commands { |
| 61 | + Commands::List => { |
| 62 | + for (c_task_id, p_task_id) in c2p.iter() { |
| 63 | + println!("{}\t{}", c_task_id, p_task_id); |
| 64 | + } |
| 65 | + } |
| 66 | + Commands::Get { commands } => match commands { |
| 67 | + GetCommands::Coordinator { task_id } => { |
| 68 | + if let Some(coordinator_task_id) = p2c.get(&task_id) { |
| 69 | + println!("{coordinator_task_id}"); |
| 70 | + } |
| 71 | + } |
| 72 | + GetCommands::Prover { task_id } => { |
| 73 | + if let Some(prover_task_id) = c2p.get(&task_id) { |
| 74 | + println!("{prover_task_id}"); |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + Ok(()) |
| 81 | +} |
0 commit comments