Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] cargo axiom e2e test #1003

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into if we can use one package (maybe bitcode) for everything. OK for now

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(())
}