cargo_openvm/commands/
commit.rs

1use std::{
2    fs::{copy, create_dir_all},
3    path::PathBuf,
4};
5
6use clap::Parser;
7use eyre::Result;
8use openvm_circuit::arch::OPENVM_DEFAULT_INIT_FILE_NAME;
9use openvm_sdk::{fs::write_to_file_json, Sdk};
10
11use super::{RunArgs, RunCargoArgs};
12use crate::{
13    commands::{load_app_pk, load_or_build_exe, ExecutionMode},
14    util::{
15        get_app_commit_path, get_manifest_path_and_dir, get_single_target_name, get_target_dir,
16        get_target_output_dir,
17    },
18};
19
20#[derive(Parser)]
21#[command(
22    name = "commit",
23    about = "View the Bn254 commit of an OpenVM executable"
24)]
25pub struct CommitCmd {
26    #[arg(
27        long,
28        action,
29        help = "Path to app proving key, by default will be ${target_dir}/openvm/app.pk",
30        help_heading = "OpenVM Options"
31    )]
32    pub app_pk: Option<PathBuf>,
33
34    #[arg(
35        long,
36        action,
37        help = "Path to OpenVM executable, if specified build will be skipped",
38        help_heading = "OpenVM Options"
39    )]
40    pub exe: Option<PathBuf>,
41
42    #[arg(
43        long,
44        help = "Path to the OpenVM config .toml file that specifies the VM extensions, by default will search for the file at ${manifest_dir}/openvm.toml",
45        help_heading = "OpenVM Options"
46    )]
47    pub config: Option<PathBuf>,
48
49    #[arg(
50        long,
51        help = "Output directory that OpenVM proving artifacts will be copied to",
52        help_heading = "OpenVM Options"
53    )]
54    pub output_dir: Option<PathBuf>,
55
56    #[arg(
57        long,
58        default_value = OPENVM_DEFAULT_INIT_FILE_NAME,
59        help = "Name of the init file",
60        help_heading = "OpenVM Options"
61    )]
62    pub init_file_name: String,
63
64    #[command(flatten)]
65    cargo_args: RunCargoArgs,
66}
67
68impl CommitCmd {
69    pub fn run(&self) -> Result<()> {
70        let app_pk = load_app_pk(&self.app_pk, &self.cargo_args)?;
71
72        let run_args = RunArgs {
73            exe: self.exe.clone(),
74            config: self.config.clone(),
75            output_dir: self.output_dir.clone(),
76            init_file_name: self.init_file_name.clone(),
77            input: None,
78            mode: ExecutionMode::Pure,
79        };
80        let (exe, target_name_stem) = load_or_build_exe(&run_args, &self.cargo_args)?;
81        let sdk = Sdk::new(app_pk.app_config())?.with_app_pk(app_pk);
82
83        let app_commit = sdk.app_prover(exe)?.app_commit();
84        println!("exe commit: {:?}", app_commit.app_exe_commit.to_bn254());
85        println!("vm commit: {:?}", app_commit.app_vm_commit.to_bn254());
86
87        let (manifest_path, _) = get_manifest_path_and_dir(&self.cargo_args.manifest_path)?;
88        let target_dir = get_target_dir(&self.cargo_args.target_dir, &manifest_path);
89        let target_output_dir = get_target_output_dir(&target_dir, &self.cargo_args.profile);
90
91        // target_name_stem does not contain "examples/" prefix
92        let target_name =
93            get_single_target_name(&self.cargo_args).unwrap_or(target_name_stem.into());
94        let commit_path = get_app_commit_path(&target_output_dir, target_name);
95
96        println!("Writing app commit to {}", commit_path.display());
97        write_to_file_json(&commit_path, app_commit)?;
98        if let Some(output_dir) = &self.output_dir {
99            create_dir_all(output_dir)?;
100            let commit_name = commit_path.file_name().unwrap();
101            let to_path = output_dir.join(commit_name);
102            copy(commit_path, to_path)?;
103        }
104
105        Ok(())
106    }
107}