Skip to content

Commit

Permalink
CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Aug 17, 2023
1 parent 8da8c4c commit 2aa262d
Showing 1 changed file with 40 additions and 54 deletions.
94 changes: 40 additions & 54 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ use cairo_lang_sierra_to_casm::compiler::CompilationError;
use cairo_lang_sierra_to_casm::metadata::MetadataError;
use cairo_lang_sierra_to_casm::{compiler::compile, metadata::calc_metadata};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_vm::air_public_input::PublicInputError;
use cairo_vm::cairo_run;
use cairo_vm::cairo_run::EncodeTraceError;
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::errors::memory_errors::MemoryError;
use cairo_vm::vm::errors::runner_errors::RunnerError;
use cairo_vm::vm::errors::trace_errors::TraceError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::{
felt::Felt252,
serde::deserialize_program::ReferenceManager,
Expand All @@ -23,17 +29,12 @@ use cairo_vm::{
vm_core::VirtualMachine,
},
};
use clap::{CommandFactory, Parser, ValueHint};
use itertools::chain;
use std::io::BufWriter;
use std::{collections::HashMap, io, path::Path};
use cairo_vm::air_public_input::PublicInputError;
use cairo_vm::cairo_run::EncodeTraceError;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::errors::trace_errors::TraceError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use clap::{CommandFactory, Parser, ValueHint};
use std::io::Write;
use std::path::PathBuf;
use std::{collections::HashMap, io, path::Path};
use thiserror::Error;

#[cfg(feature = "with_mimalloc")]
Expand Down Expand Up @@ -105,6 +106,8 @@ enum Error {
Metadata(#[from] MetadataError),
#[error(transparent)]
Program(#[from] ProgramError),
#[error(transparent)]
Memory(#[from] MemoryError),
}

pub struct FileWriter {
Expand Down Expand Up @@ -140,17 +143,15 @@ impl FileWriter {
}

Check warning on line 143 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L141-L143

Added lines #L141 - L143 were not covered by tests
}


fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
let args = Args::try_parse_from(args)?;

Check warning on line 147 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L146-L147

Added lines #L146 - L147 were not covered by tests

let compiler_config = CompilerConfig {
replace_ids: true,
..CompilerConfig::default()
};
let sierra_program =
(*compile_cairo_project_at_path(Path::new("fibonacci.cairo"), compiler_config).unwrap())
.clone();
(*compile_cairo_project_at_path(&args.filename, compiler_config).unwrap()).clone();

// variable needed for the SierraCasmRunner
let contracts_info = OrderedHashMap::default();

Check warning on line 157 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L149-L157

Added lines #L149 - L157 were not covered by tests
Expand All @@ -167,8 +168,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {

// Entry code and footer are part of the whole instructions that are
// ran by the VM.
let (entry_code, builtins) = casm_runner
.create_entry_code(main_func, &[], initial_gas)?;
let (entry_code, builtins) = casm_runner.create_entry_code(main_func, &[], initial_gas)?;
let footer = casm_runner.create_code_footer();

let check_gas_usage = true;
Expand Down Expand Up @@ -213,7 +213,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
data_len,
};

additional_initialization(function_context);
additional_initialization(function_context)?;

Check warning on line 216 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L211-L216

Added lines #L211 - L216 were not covered by tests

let mut hint_processor = CairoHintProcessor {
runner: None,
Expand All @@ -222,72 +222,58 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
run_resources: RunResources::default(),
};

runner
.run_until_pc(end, &mut vm, &mut hint_processor)?;
runner
.end_run(true, false, &mut vm, &mut hint_processor)?;

runner.run_until_pc(end, &mut vm, &mut hint_processor)?;
runner.end_run(true, false, &mut vm, &mut hint_processor)?;
runner.relocate(&mut vm, true)?;

Check warning on line 227 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L218-L227

Added lines #L218 - L227 were not covered by tests

let relocated_trace = vm.get_relocated_trace()?;

let trace_path = "test.trace";
let trace_file = std::fs::File::create(trace_path)?;
let mut trace_writer =
FileWriter::new(io::BufWriter::with_capacity(3 * 1024 * 1024, trace_file));

cairo_run::write_encoded_trace(relocated_trace, &mut trace_writer)?;
trace_writer.flush()?;

let memory_path = "test.memory";
let memory_file = std::fs::File::create(memory_path).unwrap();
let mut memory_writer =
FileWriter::new(io::BufWriter::with_capacity(5 * 1024 * 1024, memory_file));

cairo_run::write_encoded_memory(&runner.relocated_memory, &mut memory_writer)?;
memory_writer.flush().unwrap();

if args.trace_file.is_some() {
let trace_path = args.trace_file.unwrap();
let trace_file = std::fs::File::create(trace_path)?;
let mut trace_writer =
FileWriter::new(io::BufWriter::with_capacity(3 * 1024 * 1024, trace_file));

cairo_run::write_encoded_trace(relocated_trace, &mut trace_writer)?;
trace_writer.flush()?;
}
if args.memory_file.is_some() {
let memory_path = args.memory_file.unwrap();
let memory_file = std::fs::File::create(memory_path).unwrap();
let mut memory_writer =
FileWriter::new(io::BufWriter::with_capacity(5 * 1024 * 1024, memory_file));

cairo_run::write_encoded_memory(&runner.relocated_memory, &mut memory_writer)?;
memory_writer.flush().unwrap();
}
println!();
println!("Cairo1 program ran successfully");

Ok(())
}

Check warning on line 252 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L229-L252

Added lines #L229 - L252 were not covered by tests


fn additional_initialization(context: RunFunctionContext) {
fn additional_initialization(context: RunFunctionContext) -> Result<(), Error> {
let vm = context.vm;
// Create the builtin cost segment, with dummy values.
// Create the builtin cost segment
let builtin_cost_segment = vm.add_memory_segment();
for token_type in CostTokenType::iter_precost() {
vm.insert_value(
(builtin_cost_segment + (token_type.offset_in_builtin_costs() as usize)).unwrap(),
Felt252::from(token_gas_cost(*token_type)),
)
.unwrap()
)?

Check warning on line 262 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L254-L262

Added lines #L254 - L262 were not covered by tests
}
// Put a pointer to the builtin cost segment at the end of the program (after the
// additional `ret` statement).
vm.insert_value(
(vm.get_pc() + context.data_len).unwrap(),
builtin_cost_segment,
)
.unwrap();
}
)?;

Check warning on line 269 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L266-L269

Added lines #L266 - L269 were not covered by tests

#[allow(dead_code)]
fn print_bytecode(data: &[MaybeRelocatable]) {
println!();
println!("-------------- BYTECODE INSTRUCTIONS ----------------");
println!();
data.iter()
.enumerate()
.for_each(|(i, d)| println!("INSTRUCTION {}: {:?}", i, d));
Ok(())
}

Check warning on line 272 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L271-L272

Added lines #L271 - L272 were not covered by tests


fn main() -> Result<(), Error> {
match run(std::env::args()) {
Err(Error::Cli(err)) => err.exit(),
other => other,
}
}
}

Check warning on line 279 in cairo1-run/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cairo1-run/src/main.rs#L274-L279

Added lines #L274 - L279 were not covered by tests

0 comments on commit 2aa262d

Please sign in to comment.