Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MdTeach committed Jan 29, 2025
1 parent 922c267 commit d4188ec
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
6 changes: 6 additions & 0 deletions bin/datatool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ELF-PATH>
```
2 changes: 1 addition & 1 deletion bin/datatool/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub(crate) struct SubcParams {
pub(crate) proof_timeout: Option<u32>,

#[argh(option, description = "directory to export the generated ELF")]
pub(crate) elf_path: Option<PathBuf>,
pub(crate) elf_dir: Option<PathBuf>,
}

pub(crate) struct CmdContext {
Expand Down
2 changes: 1 addition & 1 deletion bin/datatool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand Down
83 changes: 52 additions & 31 deletions provers/sp1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
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<P: AsRef<Path>>(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)?;
}
}
}
}
}
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(())
}

0 comments on commit d4188ec

Please sign in to comment.