From 7b1a6117201d27b5b734eca63b4ab78a291ea755 Mon Sep 17 00:00:00 2001 From: sergerad Date: Fri, 15 Nov 2024 21:55:09 +1300 Subject: [PATCH] Add most of prove_rpc.rs logic --- Cargo.lock | 81 +++++++++++++++++++++++++++++- scripts/Cargo.toml | 1 + scripts/prove_rpc.rs | 116 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 193 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60ea5be75..268462b69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3168,6 +3168,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -4849,6 +4858,20 @@ dependencies = [ "syn 2.0.77", ] +[[package]] +name = "sysinfo" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b5ae3f4f7d64646c46c4cae4e3f01d1c5d255c7406fdd7c7f999a94e488791" +dependencies = [ + "core-foundation-sys", + "libc", + "memchr", + "ntapi", + "rayon", + "windows", +] + [[package]] name = "tap" version = "1.0.1" @@ -5651,17 +5674,70 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "windows-registry" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" dependencies = [ - "windows-result", + "windows-result 0.2.0", "windows-strings", "windows-targets 0.52.6", ] +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.2.0" @@ -5677,7 +5753,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" dependencies = [ - "windows-result", + "windows-result 0.2.0", "windows-targets 0.52.6", ] @@ -5884,6 +5960,7 @@ dependencies = [ "clap", "serde", "serde_json", + "sysinfo", ] [[package]] diff --git a/scripts/Cargo.toml b/scripts/Cargo.toml index 3df7a88a6..75727776a 100644 --- a/scripts/Cargo.toml +++ b/scripts/Cargo.toml @@ -15,6 +15,7 @@ anyhow.workspace = true clap = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] } serde_json.workspace = true +sysinfo = "0.32.0" [lints] workspace = true diff --git a/scripts/prove_rpc.rs b/scripts/prove_rpc.rs index 7c5b75208..133fe12f8 100644 --- a/scripts/prove_rpc.rs +++ b/scripts/prove_rpc.rs @@ -1,11 +1,13 @@ use std::{ env::set_var, - fs::create_dir_all, + fmt::Display, + fs::{create_dir_all, File}, path::{Path, PathBuf}, + process::{Command, Stdio}, }; use alloy::{eips::BlockId, transports::http::reqwest::Url}; -use anyhow::Result; +use anyhow::{Context as _, Result}; use clap::{arg, Args, ValueEnum, ValueHint}; #[derive(ValueEnum, Clone)] @@ -14,6 +16,15 @@ enum RpcType { Native, } +impl Display for RpcType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + RpcType::Jerigon => write!(f, "jerigon"), + RpcType::Native => write!(f, "native"), + } + } +} + #[derive(ValueEnum, Clone)] enum RunMode { /// Dummy proof is generated. Useful for quickly testing decoding and @@ -69,6 +80,19 @@ pub fn prove_via_rpc(args: ProveRpcArgs) -> Result<()> { // See also .cargo/config.toml. set_var("RUSTFLAGS", "-C target-cpu=native -Zlinker-features=-lld"); + // TODO: move this logic below when full match is done + if let RunMode::Test = args.mode { + set_var("ARITHMETIC_CIRCUIT_SIZE", "16..21"); + set_var("BYTE_PACKING_CIRCUIT_SIZE", "8..21"); + set_var("CPU_CIRCUIT_SIZE", "8..21"); + set_var("KECCAK_CIRCUIT_SIZE", "4..20"); + set_var("KECCAK_SPONGE_CIRCUIT_SIZE", "8..17"); + set_var("LOGIC_CIRCUIT_SIZE", "4..21"); + set_var("MEMORY_CIRCUIT_SIZE", "17..24"); + set_var("MEMORY_BEFORE_CIRCUIT_SIZE", "16..23"); + set_var("MEMORY_AFTER_CIRCUIT_SIZE", "7..23"); + } + // Handle optional block inputs. let start_block = args.start_block; let end_block = args.end_block.unwrap_or(start_block); @@ -92,6 +116,92 @@ pub fn prove_via_rpc(args: ProveRpcArgs) -> Result<()> { } let output_log_path = proof_output_dirpath.join(format!("b{}_{}.log", args.start_block, end_block)); + let log_out = File::create(&output_log_path).context("couldn't create log file")?; + let log_err = log_out.try_clone().context("couldn't clone log file")?; - todo!() + /// Set file handle limit. + const RECOMMENDED_FILE_LIMIT: isize = 8192; + if !sysinfo::set_open_files_limit(RECOMMENDED_FILE_LIMIT) { + eprintln!("WARNING: Unable to set file descriptor limit to recommended value: {RECOMMENDED_FILE_LIMIT}."); + } + + let runner = Runner::new("cargo") + .args(&[ + "run", + "--release", + "--package=zero", + "--bin=leader", + "--", + "--runtime=in-memory", + "--load-strategy=on-demand", + "--proof-output-dir", + proof_output_dirpath.to_str().unwrap(), + "--block-batch-size", + &args.block_batch_size.to_string(), + "rpc", + "--rpc-type", + &args.rpc_type.to_string(), + "--rpc-url", + args.rpc_url.as_ref(), + "--start-block", + &start_block.to_string(), + "--checkpoint-block", + &checkpoint_block.to_string(), + "--end-block", + &end_block.to_string(), + "--backoff", + &args.backoff.to_string(), + "--max-retries", + &args.max_retries.to_string(), + ]) + .out(log_out) + .err(log_err); + match args.mode { + RunMode::Test => runner.args(&["--use-test-config"]).run(), + RunMode::Prove => todo!(), + RunMode::Verify => todo!(), + } +} + +struct Runner { + cmd: String, + args: Vec, + out: Stdio, + err: Stdio, +} + +impl Runner { + fn new(cmd: impl Into) -> Self { + Self { + cmd: cmd.into(), + args: vec![], + out: Stdio::piped(), + err: Stdio::piped(), + } + } + + fn args(mut self, args: &[&str]) -> Self { + self.args.extend(args.iter().map(|s| s.to_string())); + self + } + + fn out(mut self, out: impl Into) -> Self { + self.out = out.into(); + self + } + + fn err(mut self, err: impl Into) -> Self { + self.err = err.into(); + self + } + + fn run(self) -> Result<()> { + let output = Command::new(&self.cmd) + .args(&self.args) + .stdout(self.out) + .stderr(self.err) + .output() + .context(format!("couldn't exec `{}`", &self.cmd))?; + todo!() + } }