Skip to content

Commit

Permalink
Split runner module and add verify path
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Nov 15, 2024
1 parent 7b1a611 commit 4abfbc0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 69 deletions.
110 changes: 41 additions & 69 deletions scripts/prove_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::{
env::set_var,
fmt::Display,
fs::{create_dir_all, File},
fs::create_dir_all,
path::{Path, PathBuf},
process::{Command, Stdio},
};

use alloy::{eips::BlockId, transports::http::reqwest::Url};
use anyhow::{Context as _, Result};
use anyhow::Result;
use clap::{arg, Args, ValueEnum, ValueHint};

use crate::runner::Runner;

#[derive(ValueEnum, Clone)]
enum RpcType {
Jerigon,
Expand Down Expand Up @@ -80,19 +81,6 @@ 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);
Expand All @@ -114,18 +102,16 @@ pub fn prove_via_rpc(args: ProveRpcArgs) -> Result<()> {
if !proof_output_dirpath.exists() {
create_dir_all(proof_output_dirpath)?;
}
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")?;

/// 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")
let log_output_filepath =
proof_output_dirpath.join(format!("b{}_{}.log", start_block, end_block));

let proof_runner = Runner::new("cargo")
.args(&[
"run",
"--release",
Expand Down Expand Up @@ -154,54 +140,40 @@ pub fn prove_via_rpc(args: ProveRpcArgs) -> Result<()> {
"--max-retries",
&args.max_retries.to_string(),
])
.out(log_out)
.err(log_err);
.out(log_output_filepath)?;
match args.mode {
RunMode::Test => runner.args(&["--use-test-config"]).run(),
RunMode::Prove => todo!(),
RunMode::Verify => todo!(),
}
}

struct Runner {
cmd: String,
args: Vec<String>,
out: Stdio,
err: Stdio,
}

impl Runner {
fn new(cmd: impl Into<String>) -> Self {
Self {
cmd: cmd.into(),
args: vec![],
out: Stdio::piped(),
err: Stdio::piped(),
RunMode::Test => {
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");
proof_runner.args(&["--test-only"]).run()
}
RunMode::Prove => proof_runner.args(&["--use-test-config"]).run(),
RunMode::Verify => {
// Generate the proof.
proof_runner.args(&["--use-test-config"]).run()?;

// Verify the proof.
let proof_filepath = proof_output_dirpath.join(format!("b{end_block}.proof"));
let verify_output_filepath = proof_output_dirpath.join("verify.out");
let verify_runner = Runner::new("cargo")
.args(&[
"run",
"--release",
"--package=zero",
"--bin=verifier",
"--",
"-f",
proof_filepath.to_str().unwrap(),
])
.out(verify_output_filepath)?;
verify_runner.run()
}
}

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<Stdio>) -> Self {
self.out = out.into();
self
}

fn err(mut self, err: impl Into<Stdio>) -> 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!()
}
}
53 changes: 53 additions & 0 deletions scripts/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
fs::File,
path::PathBuf,
process::{Command, Stdio},
};

use anyhow::{Context as _, Result};

pub struct Runner {
cmd: String,
args: Vec<String>,
stdout: Stdio,
stderr: Stdio,
}

impl Runner {
/// Create a new runner with the given command.
pub fn new(cmd: impl Into<String>) -> Self {
Self {
cmd: cmd.into(),
args: vec![],
stdout: Stdio::piped(),
stderr: Stdio::piped(),
}
}

/// Add arguments to the command.
pub fn args(mut self, args: &[&str]) -> Self {
self.args.extend(args.iter().map(|s| s.to_string()));
self
}

/// Create the file specified by `output_filepath` and set it as the stdout
/// and stderr of the command.
pub fn out(mut self, output_filepath: impl Into<PathBuf>) -> Result<Self> {
let out = File::create(output_filepath.into())?;
let err = out.try_clone()?;
self.stdout = Stdio::from(out);
self.stderr = Stdio::from(err);
Ok(self)
}

/// Run the command.
pub fn run(self) -> Result<()> {
let output = Command::new(&self.cmd)

Check failure on line 45 in scripts/runner.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `output`

Check failure on line 45 in scripts/runner.rs

View workflow job for this annotation

GitHub Actions / udeps

unused variable: `output`

Check failure on line 45 in scripts/runner.rs

View workflow job for this annotation

GitHub Actions / outdated

unused variable: `output`
.args(&self.args)
.stdout(self.stdout)
.stderr(self.stderr)
.output()
.context(format!("couldn't exec `{}`", &self.cmd))?;
todo!()
}
}
1 change: 1 addition & 0 deletions scripts/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod outdated;
mod prove_rpc;
mod runner;

use anyhow::Result;
use clap::Parser;
Expand Down

0 comments on commit 4abfbc0

Please sign in to comment.