Skip to content

Commit

Permalink
[feat] cargo axiom e2e test (#1003)
Browse files Browse the repository at this point in the history
* Add transpiler config read

* Fix exe rw

* Attempt at adding a workflow

* Change the test so that it's now not a workflow
  • Loading branch information
Golovanov399 authored Dec 12, 2024
1 parent 835d7b3 commit c7d8c8f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 20 deletions.
9 changes: 7 additions & 2 deletions crates/axvm-sdk/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs::{create_dir_all, write, File},
io::Write,
path::Path,
};

Expand All @@ -15,11 +16,15 @@ use crate::{
};

pub fn read_exe_from_file<P: AsRef<Path>>(path: P) -> Result<AxVmExe<F>> {
read_from_file_bson(path)
let data = std::fs::read(path)?;
let exe = bincode::serde::decode_from_slice(&data, bincode::config::standard())?.0;
Ok(exe)
}

pub fn write_exe_to_file<P: AsRef<Path>>(exe: AxVmExe<F>, path: P) -> Result<()> {
write_to_file_bson(path, exe)
let data = bincode::serde::encode_to_vec(&exe, bincode::config::standard())?;
File::create(path)?.write_all(&data)?;
Ok(())
}

pub fn read_app_pk_from_file<VC: VmConfig<F>, P: AsRef<Path>>(
Expand Down
45 changes: 27 additions & 18 deletions crates/cargo-axiom/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ use axvm_build::{
build_guest_package, find_unique_executable, get_package, GuestOptions, TargetFilter,
};
use axvm_rv32im_transpiler::{Rv32ITranspilerExtension, Rv32MTranspilerExtension};
use axvm_sdk::{fs::write_exe_to_file, Sdk};
use axvm_sdk::{
config::{AppConfig, SdkVmConfig},
fs::write_exe_to_file,
Sdk,
};
use axvm_transpiler::{axvm_platform::memory::MEM_SIZE, elf::Elf, transpiler::Transpiler};
use clap::Parser;
use eyre::Result;

use crate::util::read_to_struct_toml;

#[derive(Parser)]
#[command(name = "build", about = "Compile an axVM program")]
pub struct BuildCmd {
Expand Down Expand Up @@ -49,9 +55,13 @@ pub struct BuildArgs {
#[arg(long, default_value = "false")]
pub transpile: bool,

/// Path to the SDK config .toml file that specifies the transpiler extensions
#[arg(long)]
pub transpiler_config: Option<PathBuf>,

/// Output path for the transpiled program (default: <ELF base path>.axvmexe)
#[arg(long)]
pub transpile_path: Option<PathBuf>,
pub transpile_to: Option<PathBuf>,

/// Build profile
#[arg(long, default_value = "release")]
Expand All @@ -60,7 +70,7 @@ pub struct BuildArgs {

impl BuildArgs {
pub fn exe_path(&self, elf_path: &Path) -> PathBuf {
self.transpile_path
self.transpile_to
.clone()
.unwrap_or_else(|| elf_path.with_extension("axvmexe"))
}
Expand Down Expand Up @@ -116,7 +126,20 @@ pub(crate) fn build(build_args: &BuildArgs) -> Result<Option<PathBuf>> {
let elf_path = elf_path?;
println!("[axiom] Transpiling the package...");
let output_path = build_args.exe_path(&elf_path);
transpile(elf_path.clone(), output_path.clone())?;
let transpiler = if let Some(transpiler_config) = build_args.transpiler_config.clone() {
let app_config: AppConfig<SdkVmConfig> = read_to_struct_toml(&transpiler_config)?;
app_config.app_vm_config.transpiler()
} else {
Transpiler::default()
.with_extension(Rv32ITranspilerExtension)
.with_extension(Rv32MTranspilerExtension)
};

let data = read(elf_path.clone())?;
let elf = Elf::decode(&data, MEM_SIZE as u32)?;
let exe = Sdk.transpile(elf, transpiler)?;
write_exe_to_file(exe, &output_path)?;

println!(
"[axiom] Successfully transpiled to {}",
output_path.display()
Expand All @@ -133,17 +156,3 @@ pub(crate) fn build(build_args: &BuildArgs) -> Result<Option<PathBuf>> {
Ok(None)
}
}

fn transpile(elf_path: PathBuf, output_path: PathBuf) -> Result<()> {
let data = read(elf_path.clone())?;
let elf = Elf::decode(&data, MEM_SIZE as u32)?;
let exe = Sdk.transpile(
elf,
Transpiler::default()
.with_extension(Rv32ITranspilerExtension)
.with_extension(Rv32MTranspilerExtension),
)?;
write_exe_to_file(exe, &output_path)?;
eprintln!("Successfully transpiled to {}", output_path.display());
Ok(())
}
3 changes: 3 additions & 0 deletions crates/cargo-axiom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ pub fn is_supported_target() -> bool {
pub fn get_target() -> String {
target_lexicon::HOST.to_string()
}

#[cfg(test)]
mod tests;
99 changes: 99 additions & 0 deletions crates/cargo-axiom/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::{env, process::Command};

use eyre::Result;
use tempfile::tempdir;

#[test]
fn test_cli_e2e() -> Result<()> {
let temp_dir = tempdir()?;
let package_dir = env::current_dir()?;
let prefix = "[test cli e2e]";
let run_cmd = |program: &str, args: &[&str]| {
println!("{prefix} Running command: {} {} ...", program, args[0]);
let mut cmd = Command::new(program);
cmd.args(args);
cmd.current_dir(&package_dir);
let output = cmd.output().unwrap();
println!("{prefix} Finished!");
println!("{prefix} stdout:");
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
println!("{prefix} stderr:");
println!("{}", std::str::from_utf8(&output.stderr).unwrap());
};
run_cmd("cargo", &["install", "--path", ".", "--force"]);
let temp_exe = temp_dir.path().join("example.axvmexe");
let temp_pk = temp_dir.path().join("example.pk");
let temp_vk = temp_dir.path().join("example.vk");
let temp_proof = temp_dir.path().join("example.apppf");

run_cmd(
"cargo",
&[
"axiom",
"build",
"--manifest-dir",
"../axvm-sdk/example",
"--transpile",
"--transpiler-config",
"example/app_config.toml",
"--transpile-to",
temp_exe.to_str().unwrap(),
],
);

run_cmd(
"cargo",
&[
"axiom",
"keygen",
"--config",
"example/app_config.toml",
"--output",
temp_pk.to_str().unwrap(),
"--vk-output",
temp_vk.to_str().unwrap(),
],
);

run_cmd(
"cargo",
&[
"axiom",
"run",
"--exe",
temp_exe.to_str().unwrap(),
"--config",
"example/app_config.toml",
],
);

run_cmd(
"cargo",
&[
"axiom",
"prove",
"app",
"--app-pk",
temp_pk.to_str().unwrap(),
"--exe",
temp_exe.to_str().unwrap(),
"--output",
temp_proof.to_str().unwrap(),
],
);

run_cmd(
"cargo",
&[
"axiom",
"verify",
"app",
"--app-vk",
temp_vk.to_str().unwrap(),
"--proof",
temp_proof.to_str().unwrap(),
],
);

Ok(())
}

0 comments on commit c7d8c8f

Please sign in to comment.