From d4188ecf78a2ed3233f4799685eb5fcc995db47b Mon Sep 17 00:00:00 2001 From: abishek Date: Thu, 30 Jan 2025 03:48:20 +0545 Subject: [PATCH] update readme --- bin/datatool/README.md | 6 +++ bin/datatool/src/args.rs | 2 +- bin/datatool/src/util.rs | 2 +- provers/sp1/src/lib.rs | 83 +++++++++++++++++++++++++--------------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/bin/datatool/README.md b/bin/datatool/README.md index cfbae6fedb..451c93b615 100644 --- a/bin/datatool/README.md +++ b/bin/datatool/README.md @@ -48,3 +48,9 @@ Because building the guest code in Docker can be time-consuming, you can generat ```bash cargo build --bin strata-datatool -F "sp1" --release ``` + +Additionally, the generated ELF can be exported after building the datatool as specified above: + +```bash +strata-datatool genparams --elf-path +``` diff --git a/bin/datatool/src/args.rs b/bin/datatool/src/args.rs index e15e3d55ef..670e3cb2ea 100644 --- a/bin/datatool/src/args.rs +++ b/bin/datatool/src/args.rs @@ -187,7 +187,7 @@ pub(crate) struct SubcParams { pub(crate) proof_timeout: Option, #[argh(option, description = "directory to export the generated ELF")] - pub(crate) elf_path: Option, + pub(crate) elf_dir: Option, } pub(crate) struct CmdContext { diff --git a/bin/datatool/src/util.rs b/bin/datatool/src/util.rs index efdc308786..b0323f6cf9 100644 --- a/bin/datatool/src/util.rs +++ b/bin/datatool/src/util.rs @@ -298,7 +298,7 @@ fn exec_genparams(cmd: SubcParams, ctx: &mut CmdContext) -> anyhow::Result<()> { println!("{params_buf}"); } - if let Some(elf_path) = &cmd.elf_path { + if let Some(elf_path) = &cmd.elf_dir { export_elf(elf_path)?; } diff --git a/provers/sp1/src/lib.rs b/provers/sp1/src/lib.rs index 392d7afffd..3219bf835e 100644 --- a/provers/sp1/src/lib.rs +++ b/provers/sp1/src/lib.rs @@ -1,42 +1,51 @@ -use std::path::PathBuf; +use std::{fs, path::Path}; -use anyhow::{Context, Result}; +use anyhow::{Context, Ok, Result}; include!(concat!(env!("OUT_DIR"), "/methods.rs")); -pub fn export_elf(elf_path: &PathBuf) -> Result<()> { - // Create output directory if it doesn't exist - fs::create_dir_all(elf_path).context("Failed to create ELF output directory")?; +/// Exports ELF files to the specified directory. +/// +/// Creates the output directory if it doesn't exist and copies all `.elf` files +/// from guest program directories into it. +/// +/// # Arguments +/// +/// * `elf_path` - The path to the directory where ELF files will be exported. +/// +/// # Errors +/// +/// Returns an error if directory creation or file operations fail. +pub fn export_elf>(elf_path: P) -> Result<()> { + let elf_path = elf_path.as_ref(); + fs::create_dir_all(elf_path) + .with_context(|| format!("Failed to create ELF output directory {:?}", elf_path))?; - // Get the current crate's directory - let cargo_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let sp1_builder_dir = Path::new(env!("CARGO_MANIFEST_DIR")); - // Iterate over entries in cargo_dir - for entry in fs::read_dir(&cargo_dir) - .with_context(|| format!("Failed to read directory {:?}", cargo_dir))? + for entry in fs::read_dir(sp1_builder_dir) + .with_context(|| format!("Failed to read directory {:?}", sp1_builder_dir))? { - let entry = entry?; + let entry = entry.with_context(|| "Failed to read directory entry")?; let path = entry.path(); - if path.is_dir() { - if let Some(folder_name) = path.file_name().and_then(|n| n.to_str()) { - if folder_name.starts_with("guest-") { - let cache_dir = path.join("cache"); - if cache_dir.is_dir() { - for file in fs::read_dir(&cache_dir).with_context(|| { - format!("Failed to read cache directory {:?}", cache_dir) - })? { - let file = file?; - let file_path = file.path(); - if file_path.is_file() - && file_path.extension().and_then(|s| s.to_str()) == Some("elf") - { - let file_name = file_path.file_name().unwrap(); - let dest_file = elf_path.join(file_name); - fs::copy(&file_path, &dest_file).with_context(|| { - format!("Failed to copy {:?} to {:?}", file_path, dest_file) - })?; - } - } + migrate_guest_program(&path, elf_path)?; + } + + Ok(()) +} + +fn migrate_guest_program(path: &Path, elf_path: &Path) -> Result<()> { + if path.is_dir() { + if let Some(folder_name) = path.file_name().and_then(|n| n.to_str()) { + if folder_name.starts_with("guest-") { + let cache_dir = path.join("cache"); + if cache_dir.is_dir() { + for file in fs::read_dir(&cache_dir).with_context(|| { + format!("Failed to read cache directory {:?}", cache_dir) + })? { + let file = file.with_context(|| "Failed to read cache file entry")?; + let file_path = file.path(); + migrate_elf(&file_path, elf_path)?; } } } @@ -44,3 +53,15 @@ pub fn export_elf(elf_path: &PathBuf) -> Result<()> { } Ok(()) } + +fn migrate_elf(file_path: &Path, elf_path: &Path) -> Result<()> { + if file_path.is_file() && file_path.extension().and_then(|s| s.to_str()) == Some("elf") { + let file_name = file_path + .file_name() + .with_context(|| format!("Failed to get file name from {:?}", file_path))?; + let dest_file = elf_path.join(file_name); + fs::copy(file_path, &dest_file) + .with_context(|| format!("Failed to copy {:?} to {:?}", file_path, dest_file))?; + } + Ok(()) +}