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

Add cairo0_run binary #402

Merged
merged 10 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resolver = "2"
members = [
"crates/bin/hint_tool",
"crates/bin/prove_block",
"crates/bin/cairo0_run",
"crates/cairo-type-derive",
"crates/rpc-client",
"crates/rpc-replay",
Expand Down
32 changes: 32 additions & 0 deletions crates/bin/cairo0_run/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "cairo0_run"
version.workspace = true
edition.workspace = true
repository.workspace = true
license-file.workspace = true


[dependencies]
anyhow = { workspace = true }
blockifier = { workspace = true }
cairo-lang-starknet-classes = { workspace = true }
cairo-lang-utils = { workspace = true }
cairo-vm = { workspace = true }
clap = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }
futures-core = { workspace = true }
pathfinder-common = { workspace = true }
pathfinder-crypto = { workspace = true }
pathfinder-serde = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
starknet_api = { workspace = true }
starknet-os = { workspace = true }
starknet-os-types = { workspace = true }
starknet-types-core = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
num-bigint = { workspace = true }
87 changes: 87 additions & 0 deletions crates/bin/cairo0_run/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use cairo_vm::cairo_run::CairoRunConfig;
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::types::program::Program;
use cairo_vm::vm::errors::vm_exception::VmException;
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
use cairo_vm::Felt252;
use clap::Parser;
use starknet_os::error::SnOsError;
use starknet_os::hints;
use starknet_os::starknet::starknet_storage::{CommitmentInfo, CommitmentInfoError, PerContractStorage};
use starknet_os::starkware_utils::commitment_tree::base_types::TreeIndex;

#[derive(Parser, Debug)]
struct Args {
#[arg(long)]
input: String,
}

fn init_logging() {
env_logger::builder()
.filter_level(log::LevelFilter::Trace)
.format_timestamp(None)
.try_init()
.expect("Failed to configure env_logger");
}

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

let args = Args::parse();

let program_bytes = std::fs::read_to_string(args.input).expect("Failed to read input file");

let layout = LayoutName::all_cairo;

// Init CairoRunConfig
let cairo_run_config = CairoRunConfig { layout, relocate_mem: true, trace_enabled: true, ..Default::default() };
let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or(false);

// Load the Starknet OS Program
let program = Program::from_bytes(program_bytes.as_bytes(), Some(cairo_run_config.entrypoint))
.map_err(|e| SnOsError::Runner(e.into()))?;

// Init cairo runner
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)
.map_err(|e| SnOsError::Runner(e.into()))?;

// Init the Cairo VM
let end = cairo_runner.initialize(allow_missing_builtins).map_err(|e| SnOsError::Runner(e.into()))?;

// Run the Cairo VM
let mut hint_processor = hints::SnosHintProcessor::<DummyPCS>::default();
whichqua marked this conversation as resolved.
Show resolved Hide resolved
cairo_runner
.run_until_pc(end, &mut hint_processor)
.map_err(|err| VmException::from_vm_error(&cairo_runner, err))
.map_err(|e| SnOsError::Runner(e.into()))?;

// End the Cairo VM run
cairo_runner
.end_run(cairo_run_config.disable_trace_padding, false, &mut hint_processor)
.map_err(|e| SnOsError::Runner(e.into()))?;

if cairo_run_config.proof_mode {
cairo_runner.finalize_segments().map_err(|e| SnOsError::Runner(e.into()))?;
}

Ok(())
}

struct DummyPCS {}

impl PerContractStorage for DummyPCS {
async fn compute_commitment(&mut self) -> Result<CommitmentInfo, CommitmentInfoError> {
unimplemented!();
}
async fn read(&mut self, _key: TreeIndex) -> Option<Felt252> {
unimplemented!();
}
fn write(&mut self, _key: TreeIndex, _value: Felt252) {
unimplemented!();
}
}
11 changes: 9 additions & 2 deletions crates/starknet-os/src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ pub const BREAKPOINT: &str = "breakpoint()";
pub fn breakpoint(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let pc = vm.get_pc();
Expand All @@ -559,6 +559,13 @@ pub fn breakpoint(
// println!("\tap_tracking -> {ap_tracking:?}");
// println!("\texec_scops -> {:?}", exec_scopes.get_local_variables().unwrap().keys());
// println!("\tids -> {:?}", ids_data);

log::debug!("\tids_data ({}):", ids_data.len());
for (i, (k, _v)) in ids_data.iter().enumerate() {
let value = get_maybe_relocatable_from_var_name(k, vm, ids_data, ap_tracking)?;
log::debug!("\t\t[{}] \"{}\": \"{:?}\"", i, k, value);
}

log::debug!("-----------END BREAKPOINT-----------");
Ok(())
}
Expand Down
Loading