From 0a82871ddc01ce135695fc310714ec4b3ba6f8ae Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 10 Oct 2024 15:34:36 -0600 Subject: [PATCH 1/6] Add cairo0_run binary --- Cargo.toml | 1 + crates/bin/cairo0_run/Cargo.toml | 38 ++++++++++++++ crates/bin/cairo0_run/src/main.rs | 82 +++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 crates/bin/cairo0_run/Cargo.toml create mode 100644 crates/bin/cairo0_run/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b2ce07c8..77ba3d55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/crates/bin/cairo0_run/Cargo.toml b/crates/bin/cairo0_run/Cargo.toml new file mode 100644 index 00000000..4b6ff85c --- /dev/null +++ b/crates/bin/cairo0_run/Cargo.toml @@ -0,0 +1,38 @@ +[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 } +rpc-client = { workspace = true } +rpc-replay = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +serde_with = { workspace = true } +starknet_api = { workspace = true } +starknet = { 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 } + +[dev-dependencies] +rstest = { workspace = true } diff --git a/crates/bin/cairo0_run/src/main.rs b/crates/bin/cairo0_run/src/main.rs new file mode 100644 index 00000000..902d5c9c --- /dev/null +++ b/crates/bin/cairo0_run/src/main.rs @@ -0,0 +1,82 @@ +use cairo_vm::Felt252; +use cairo_vm::{cairo_run::CairoRunConfig, types::layout_name::LayoutName}; +use clap::Parser; +use cairo_vm::types::program::Program; +use cairo_vm::vm::errors::vm_exception::VmException; +use cairo_vm::vm::runners::cairo_runner::CairoRunner; +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> { + 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::::default(); + 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 { unimplemented!(); } + async fn read(&mut self, key: TreeIndex) -> Option { unimplemented!(); } + fn write(&mut self, key: TreeIndex, value: Felt252) { unimplemented!(); } +} From 51b3cd95c4016ee64cd6d5c409daaedf42103117 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 10 Oct 2024 16:27:36 -0600 Subject: [PATCH 2/6] fmt --- crates/bin/cairo0_run/src/main.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/bin/cairo0_run/src/main.rs b/crates/bin/cairo0_run/src/main.rs index 902d5c9c..60c51690 100644 --- a/crates/bin/cairo0_run/src/main.rs +++ b/crates/bin/cairo0_run/src/main.rs @@ -1,9 +1,10 @@ -use cairo_vm::Felt252; -use cairo_vm::{cairo_run::CairoRunConfig, types::layout_name::LayoutName}; -use clap::Parser; +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}; @@ -37,8 +38,8 @@ fn main() -> Result<(), Box> { 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()))?; + 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( @@ -69,14 +70,18 @@ fn main() -> Result<(), Box> { } Ok(()) - } -struct DummyPCS { -} +struct DummyPCS {} impl PerContractStorage for DummyPCS { - async fn compute_commitment(&mut self) -> Result { unimplemented!(); } - async fn read(&mut self, key: TreeIndex) -> Option { unimplemented!(); } - fn write(&mut self, key: TreeIndex, value: Felt252) { unimplemented!(); } + async fn compute_commitment(&mut self) -> Result { + unimplemented!(); + } + async fn read(&mut self, key: TreeIndex) -> Option { + unimplemented!(); + } + fn write(&mut self, key: TreeIndex, value: Felt252) { + unimplemented!(); + } } From 1f4361f329ee8b05328846a44fdbd7d637f2432e Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Fri, 18 Oct 2024 15:17:50 -0600 Subject: [PATCH 3/6] Hijack breakpoint to dump ids_data --- crates/starknet-os/src/hints/mod.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/starknet-os/src/hints/mod.rs b/crates/starknet-os/src/hints/mod.rs index 8774a621..8f438461 100644 --- a/crates/starknet-os/src/hints/mod.rs +++ b/crates/starknet-os/src/hints/mod.rs @@ -536,10 +536,10 @@ pub const BREAKPOINT: &str = "breakpoint()"; pub fn breakpoint( vm: &mut VirtualMachine, - _exec_scopes: &mut ExecutionScopes, - _ids_data: &HashMap, - _ap_tracking: &ApTracking, - _constants: &HashMap, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, + constants: &HashMap, ) -> Result<(), HintError> { let pc = vm.get_pc(); let fp = vm.get_fp(); @@ -558,6 +558,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(()) } From 164015df64de7cdf9a2f697e9021633a2b30fc28 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 21 Oct 2024 13:29:09 -0600 Subject: [PATCH 4/6] fmt --- crates/starknet-os/src/hints/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet-os/src/hints/mod.rs b/crates/starknet-os/src/hints/mod.rs index 8f438461..3848871c 100644 --- a/crates/starknet-os/src/hints/mod.rs +++ b/crates/starknet-os/src/hints/mod.rs @@ -558,13 +558,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(()) } From 9171ff576e57c0c6c520f57675e53773156a4990 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 21 Oct 2024 13:41:44 -0600 Subject: [PATCH 5/6] More lints --- crates/bin/cairo0_run/src/main.rs | 4 ++-- crates/starknet-os/src/hints/mod.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bin/cairo0_run/src/main.rs b/crates/bin/cairo0_run/src/main.rs index 60c51690..7bb2a417 100644 --- a/crates/bin/cairo0_run/src/main.rs +++ b/crates/bin/cairo0_run/src/main.rs @@ -78,10 +78,10 @@ impl PerContractStorage for DummyPCS { async fn compute_commitment(&mut self) -> Result { unimplemented!(); } - async fn read(&mut self, key: TreeIndex) -> Option { + async fn read(&mut self, _key: TreeIndex) -> Option { unimplemented!(); } - fn write(&mut self, key: TreeIndex, value: Felt252) { + fn write(&mut self, _key: TreeIndex, _value: Felt252) { unimplemented!(); } } diff --git a/crates/starknet-os/src/hints/mod.rs b/crates/starknet-os/src/hints/mod.rs index 629ccc4e..0fcbc6c6 100644 --- a/crates/starknet-os/src/hints/mod.rs +++ b/crates/starknet-os/src/hints/mod.rs @@ -537,10 +537,10 @@ pub const BREAKPOINT: &str = "breakpoint()"; pub fn breakpoint( vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, + _exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, - constants: &HashMap, + _constants: &HashMap, ) -> Result<(), HintError> { let pc = vm.get_pc(); let fp = vm.get_fp(); @@ -561,7 +561,7 @@ pub fn breakpoint( // println!("\tids -> {:?}", ids_data); log::debug!("\tids_data ({}):", ids_data.len()); - for (i, (k, v)) in ids_data.iter().enumerate() { + 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); } From a3bb08bdb82118d87a538f29f3f9db2aac3cb302 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 21 Oct 2024 13:48:03 -0600 Subject: [PATCH 6/6] Remove unused deps --- crates/bin/cairo0_run/Cargo.toml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/crates/bin/cairo0_run/Cargo.toml b/crates/bin/cairo0_run/Cargo.toml index 4b6ff85c..f8549647 100644 --- a/crates/bin/cairo0_run/Cargo.toml +++ b/crates/bin/cairo0_run/Cargo.toml @@ -20,19 +20,13 @@ pathfinder-common = { workspace = true } pathfinder-crypto = { workspace = true } pathfinder-serde = { workspace = true } reqwest = { workspace = true } -rpc-client = { workspace = true } -rpc-replay = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } serde_with = { workspace = true } starknet_api = { workspace = true } -starknet = { 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 } - -[dev-dependencies] -rstest = { workspace = true }