Skip to content

Commit

Permalink
Feature: info logs (#5)
Browse files Browse the repository at this point in the history
Problem: the CLI does not display enough information about what it is
doing.

Solution: add logs. The CLI now uses the `log` and `env_logger` crate to
print messages. This will simplify the addition of a verbosity flag down
the line.
  • Loading branch information
odesenfans authored Feb 22, 2024
1 parent 445a6fd commit cd126ba
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ edition = "2021"
[dependencies]
cairo-vm = { git = "https://github.com/Moonsong-Labs/cairo-vm", rev = "b2f69d230416129a84ad8237ccc13d088992f74b", features = ["extensive_hints"] }
clap = { version = "4.5.0", features = ["derive"] }
env_logger = { version = "0.11.2", features = ["color"] }
log = "0.4.20"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = { version = "1.0.113" }
stone-prover-sdk = { git = "https://github.com/Moonsong-Labs/stone-prover-sdk", tag="v0.3.0" }
stone-prover-sdk = { git = "https://github.com/Moonsong-Labs/stone-prover-sdk", tag = "v0.3.0" }
thiserror = { version = "1.0.57" }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-stone-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eo pipefail

VERSION="v0.1.0"
VERSION="v0.1.1"
INSTALL_DIR="${HOME}/.stone/${VERSION}"
TARBALL="stone-cli-linux-x86-64.tar.gz"

Expand Down
6 changes: 6 additions & 0 deletions src/commands/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cairo_vm::types::errors::cairo_pie_error::CairoPieError;
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::types::program::Program;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use log::{debug, info};
use serde::Serialize;
use stone_prover_sdk::cairo_vm::{
extract_execution_artifacts, run_bootloader_in_proof_mode, run_in_proof_mode,
Expand Down Expand Up @@ -106,6 +107,8 @@ pub fn run_with_bootloader(args: ProveWithBootloaderArgs) -> Result<ExecutionArt
}

pub fn prove(command: ProveCommand) -> Result<(), RunError> {
debug!("preparing config files...");

// Cloning here is the easiest solution to avoid borrow checks.
let config_args = command.config().clone();

Expand All @@ -122,6 +125,7 @@ pub fn prove(command: ProveCommand) -> Result<(), RunError> {
.map(|path| read_json_from_file(path).map_err(|e| RunError::Deserialize(path.clone(), e)))
.transpose()?;

info!("execution in progress...");
let execution_artifacts = match command {
ProveCommand::Bare(args) => run_program(args)?,
ProveCommand::WithBootloader(args) => run_with_bootloader(args)?,
Expand All @@ -133,6 +137,7 @@ pub fn prove(command: ProveCommand) -> Result<(), RunError> {
last_layer_degree_bound,
));

info!("proving in progress...");
let proof = run_prover(
&execution_artifacts.public_input,
&execution_artifacts.private_input,
Expand All @@ -141,6 +146,7 @@ pub fn prove(command: ProveCommand) -> Result<(), RunError> {
&prover_config,
&prover_parameters,
)?;
info!("proving completed!");

let output_file = config_args.output_file();
write_json_to_file(proof, output_file.as_ref())
Expand Down
7 changes: 6 additions & 1 deletion src/commands/verify.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use log::info;
use stone_prover_sdk::error::VerifierError;
use stone_prover_sdk::verifier::run_verifier;

use crate::cli::VerifyArgs;

pub fn verify(args: VerifyArgs) -> Result<(), VerifierError> {
run_verifier(args.proof_file.as_path())
info!("verification in progress...");
run_verifier(args.proof_file.as_path())?;
info!("verification completed!");

Ok(())
}
30 changes: 26 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::cli::Cli;
use crate::commands::prove::RunError;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use clap::Parser;
use env_logger::fmt::Formatter;
use log::{error, LevelFilter, Record};
use std::io;
use std::io::Write;
use stone_prover_sdk::cairo_vm::ExecutionError;
use stone_prover_sdk::error::VerifierError;

use crate::cli::Cli;
use crate::commands::prove::RunError;

mod cli;
mod commands;
mod toolkit;
Expand All @@ -18,6 +21,23 @@ enum CliError {
Verify(#[from] VerifierError),
}

fn format_log(buf: &mut Formatter, record: &Record) -> io::Result<()> {
let level_style = buf.default_level_style(record.level());
writeln!(
buf,
"{level_style}{:<5}{level_style:#} {}",
record.level().to_string().to_lowercase(),
record.args()
)
}

fn setup_logging() {
env_logger::Builder::new()
.filter_level(LevelFilter::Info)
.format(format_log)
.init();
}

fn display_error(error: CliError) {
let error_message = match error {
CliError::Prove(run_error) => match run_error {
Expand Down Expand Up @@ -70,7 +90,7 @@ fn display_error(error: CliError) {
}
},
};
println!("Error: {}", error_message);
error!("{}", error_message);
}

fn process_cli_command(command: Cli) -> Result<(), CliError> {
Expand All @@ -83,6 +103,8 @@ fn process_cli_command(command: Cli) -> Result<(), CliError> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
setup_logging();

let command = Cli::parse();
if let Err(e) = process_cli_command(command) {
display_error(e);
Expand Down

0 comments on commit cd126ba

Please sign in to comment.