From 46206b09dded7e25eb19aab898074a21fa2e4a6f Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Fri, 19 Apr 2024 15:27:48 +0200 Subject: [PATCH] bootloader build on project build --- .cargo/config.toml | 5 +++- crates/prover/src/stone_prover/mod.rs | 24 +++++++---------- crates/runner/build.rs | 38 ++++++++++++++++++++++----- crates/runner/src/cairo_runner/mod.rs | 31 +++++++++++++++------- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index f4ab6e5..2024a54 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,5 @@ [env] -RUST_LOG = "info" \ No newline at end of file +RUST_LOG = "info" +CAIRO_PATH = "cairo" +BOOTLOADER_PATH = "bootloader/recursive_with_poseidon/simple_bootloader.cairo" +BOOTLOADER_OUT_NAME = "bootloader.json" \ No newline at end of file diff --git a/crates/prover/src/stone_prover/mod.rs b/crates/prover/src/stone_prover/mod.rs index 457d89d..cfe8042 100644 --- a/crates/prover/src/stone_prover/mod.rs +++ b/crates/prover/src/stone_prover/mod.rs @@ -31,20 +31,16 @@ impl ProverController for StoneProver { let cpu_air_params = NamedTempFile::new()?; // TODO implement default config and getting info from integrity verifier let task = Command::new("cpu_air_prover") - .args(["--out_file", out_file.path().to_string_lossy().as_ref()]) - .args([ - "--air_private_input", - job_trace.air_private_input.path().to_string_lossy().as_ref(), - ]) - .args([ - "--air_public_input", - job_trace.air_public_input.path().to_string_lossy().as_ref(), - ]) - .args([ - "--cpu_air_prover_config", - cpu_air_prover_config.path().to_string_lossy().as_ref(), - ]) - .args(["--cpu_air_params", cpu_air_params.path().to_string_lossy().as_ref()]) + .arg("--out_file") + .arg(out_file.path()) + .arg("--air_private_input") + .arg(job_trace.air_private_input.path()) + .arg("--air_public_input") + .arg(job_trace.air_public_input.path()) + .arg("--cpu_air_prover_config") + .arg(cpu_air_prover_config.path()) + .arg("--cpu_air_params") + .arg(cpu_air_params.path()) .arg("--generate_annotations") .spawn()?; diff --git a/crates/runner/build.rs b/crates/runner/build.rs index 7d14d34..887d3b1 100644 --- a/crates/runner/build.rs +++ b/crates/runner/build.rs @@ -1,15 +1,41 @@ +use std::env; +use std::path::PathBuf; use std::process::Command; fn main() { // Check if cairo-run command is present - Command::new("cairo-run") - .arg("--version") - .output() - .expect("Failed to execute cairo-run command"); + check_command("cairo-run"); // Check if cairo-compile command is present + check_command("cairo-compile"); + + let workspace_root = + PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env not present")) + .join("../../"); + let out_dir = + PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env not present")).join("../../../../"); + let cairo_path = PathBuf::from(env::var("CAIRO_PATH").expect("CAIRO_PATH env not present")); + let bootloader_path = + PathBuf::from(env::var("BOOTLOADER_PATH").expect("BOOTLOADER_PATH env not present")); + let bootloader_out_name = PathBuf::from( + env::var("BOOTLOADER_OUT_NAME").expect("BOOTLOADER_OUT_NAME env not present"), + ); + + // Compile Bootloader Command::new("cairo-compile") - .arg("--version") + .arg("--cairo_path") + .arg(&workspace_root.join(&cairo_path)) + .arg(&workspace_root.join(&cairo_path).join(&bootloader_path)) + .arg("--output") + .arg(&out_dir.join(&bootloader_out_name)) + .arg("--proof_mode") .output() - .expect("Failed to execute cairo-compile command"); + .expect("bootloader compile failed"); +} + +fn check_command(cmd: &str) { + match Command::new(cmd).arg("--version").output() { + Ok(_) => println!("{} command found", cmd), + Err(e) => panic!("Failed to execute {} command: {}", cmd, e), + } } diff --git a/crates/runner/src/cairo_runner/mod.rs b/crates/runner/src/cairo_runner/mod.rs index 1ca896f..4cb3b12 100644 --- a/crates/runner/src/cairo_runner/mod.rs +++ b/crates/runner/src/cairo_runner/mod.rs @@ -7,11 +7,11 @@ use crate::{ }, }; use sharp_p2p_common::{hash, job::Job, job_trace::JobTrace}; -use std::io::Write; use std::{ collections::HashMap, hash::{DefaultHasher, Hash, Hasher}, }; +use std::{env, io::Write, path::PathBuf}; use tempfile::NamedTempFile; use tokio::process::{Child, Command}; use tracing::{debug, trace}; @@ -28,7 +28,13 @@ impl Runner for CairoRunner { impl RunnerController for CairoRunner { async fn run(&mut self, job: Job) -> Result { - let program = NamedTempFile::new()?; + let cargo_target_dir = + PathBuf::from(env::var("CARGO_TARGET_DIR").expect("CARGO_TARGET_DIR env not present")); + let bootloader_out_name = PathBuf::from( + env::var("BOOTLOADER_OUT_NAME").expect("BOOTLOADER_OUT_NAME env not present"), + ); + + let program = cargo_target_dir.join(&bootloader_out_name); let layout: &str = Layout::RecursiveWithPoseidon.into(); let mut cairo_pie = NamedTempFile::new()?; @@ -49,13 +55,20 @@ impl RunnerController for CairoRunner { let memory = NamedTempFile::new()?; let task = Command::new("cairo-run") - .args(["--program", program.path().to_string_lossy().as_ref()]) - .args(["--layout", layout]) - .args(["--program_input", program_input.path().to_string_lossy().as_ref()]) - .args(["--air_public_input", air_public_input.path().to_string_lossy().as_ref()]) - .args(["--air_private_input", air_private_input.path().to_string_lossy().as_ref()]) - .args(["--trace_file", trace.path().to_string_lossy().as_ref()]) - .args(["--memory_file", memory.path().to_string_lossy().as_ref()]) + .arg("--program") + .arg(program.as_path()) + .arg("--layout") + .arg(layout) + .arg("--program_input") + .arg(program_input.path()) + .arg("--air_public_input") + .arg(air_public_input.path()) + .arg("--air_private_input") + .arg(air_private_input.path()) + .arg("--trace_file") + .arg(trace.path()) + .arg("--memory_file") + .arg(memory.path()) .arg("--proof_mode") .arg("--print_output") .spawn()?;