diff --git a/Cargo.lock b/Cargo.lock index 4a0a42c76..90f9b30e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1531,6 +1531,15 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" +[[package]] +name = "codegen" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff61280aed771c3070e7dcc9e050c66f1eb1e3b96431ba66f9f74641d02fc41d" +dependencies = [ + "indexmap 1.9.3", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -1624,6 +1633,12 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "coverage-helper" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fd441a9eb45a44e9dfa9ed405d4ab521f1b447ed62ef5d67ddc5a334a21b5ca" + [[package]] name = "coverage-helper" version = "0.2.0" @@ -3269,6 +3284,16 @@ dependencies = [ "phf_shared 0.11.2", ] +[[package]] +name = "phf_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" +dependencies = [ + "phf_generator", + "phf_shared 0.11.2", +] + [[package]] name = "phf_generator" version = "0.11.2" @@ -3348,19 +3373,6 @@ dependencies = [ "time", ] -[[package]] -name = "plotters" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" -dependencies = [ - "num-traits 0.2.17", - "plotters-backend", - "plotters-svg", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "plotters-backend" version = "0.3.5" @@ -4458,7 +4470,8 @@ dependencies = [ "cairo-lang-utils", "cairo-native", "cairo-vm", - "coverage-helper", + "coverage-helper 0.2.0", + "criterion", "flate2", "getset", "hex", @@ -4470,6 +4483,9 @@ dependencies = [ "num-integer", "num-traits 0.2.17", "once_cell", + "phf", + "phf_codegen", + "phf_macros", "pretty_assertions_sorted", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d14f87c95..af7e54002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,6 @@ cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "f66 cairo-vm = { workspace = true } flate2 = "1.0.25" getset = "0.1.2" -hex = "0.4.3" # TODO: Replace with sha3. We should look how to integrate it correctly to calculate sn_keccak keccak = "0.1.3" lazy_static = "1.4.0" @@ -59,6 +58,9 @@ starknet_api = { workspace = true } starknet-crypto = "0.5.1" thiserror = { workspace = true } tracing = "0.1.37" +hex = "0.4.3" +phf = "0.11" +phf_macros = "0.11" [dev-dependencies] assert_matches = "1.5.0" @@ -66,6 +68,7 @@ coverage-helper = "0.2.0" lru = "0.11.0" pretty_assertions_sorted = "1.2.3" tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } +criterion = { version = "0.5.1", features = ["html_reports"] } [[bench]] path = "bench/internals.rs" @@ -77,3 +80,9 @@ path = "bench/native_bench.rs" name = "cairo_native" required-features = ["cairo-native"] harness = false + +[[bin]] +path = "bench/native_bench.rs" +name = "cairo_native" +required-features = ["cairo-native"] +harness = false diff --git a/Makefile b/Makefile index 0c5875ac8..e7cbbd4cd 100644 --- a/Makefile +++ b/Makefile @@ -231,3 +231,6 @@ benchmark: compile-cairo compile-starknet ./scripts/bench-deploy-invoke.sh ./scripts/bench-fibonacci.sh ./scripts/bench-deploy.sh + +regenerate map-hints: + cargo run --bin starknet-rs-hint-codegen --manifest-path starknet-rs-hint-codegen/Cargo.toml \ No newline at end of file diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs new file mode 100644 index 000000000..fb0ad6ab4 --- /dev/null +++ b/bench/hashing_running_time.rs @@ -0,0 +1,70 @@ +use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic; +use cairo_vm::{ + hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, + types::exec_scope::ExecutionScopes, + vm::{runners::cairo_runner::RunResources, vm_core::VirtualMachine}, +}; +use criterion::{criterion_group, criterion_main, Criterion}; +use starknet_in_rust::syscalls::deprecated_syscall_handler::DeprecatedSyscallHintProcessor; +use starknet_in_rust::syscalls::hint_code_map; +use starknet_in_rust::{ + state::{cached_state::CachedState, in_memory_state_reader::InMemoryStateReader}, + syscalls::deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, +}; +use std::collections::HashMap; +use std::{any::Any, hint::black_box}; + +fn criterion_benchmark(c: &mut Criterion) { + let exec_scopes = &mut ExecutionScopes::new(); + let ids_names = vec!["syscall_ptr"]; + let references = { + let mut references = HashMap::< + usize, + cairo_vm::hint_processor::hint_processor_definition::HintReference, + >::new(); + for i in 0..ids_names.len() as i32 { + references.insert( + i as usize, + cairo_vm::hint_processor::hint_processor_definition::HintReference::new_simple(i), + ); + } + references + }; + + for (hint_code, hint_name) in &hint_code_map::HINTCODE { + let mut ids_data = HashMap::< + String, + cairo_vm::hint_processor::hint_processor_definition::HintReference, + >::new(); + for (i, name) in ids_names.iter().enumerate() { + ids_data.insert(name.to_string(), references.get(&i).unwrap().clone()); + } + + let hint_data: Box = Box::new(HintProcessorData::new_default( + hint_code.to_string(), + ids_data.clone(), + )); + + // Changed the string passed to bench_function to include the hint_name for unique labeling + c.bench_function(&format!("execute_hint_{:?}", hint_name), |b| { + b.iter(|| { + let mut vm = VirtualMachine::new(false); + let mut state = CachedState::::default(); + let mut syscall_handler = DeprecatedSyscallHintProcessor::new( + DeprecatedBLSyscallHandler::default_with(&mut state), + RunResources::default(), + ); + let constants = &HashMap::new(); + let _ = black_box(syscall_handler.execute_hint( + &mut vm, + exec_scopes, + &hint_data, + constants, + )); + }) + }); + } +} + +criterion_group!(bench, criterion_benchmark); +criterion_main!(bench); diff --git a/src/lib.rs b/src/lib.rs index 628c99969..7ed06f38c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,7 @@ use std::sync::Arc; #[cfg(test)] #[macro_use] extern crate assert_matches; +extern crate phf_macros; // Re-exports pub use crate::services::api::contract_classes::deprecated_contract_class::{ diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 9dc3c1bbc..db2b3f06d 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -1,6 +1,6 @@ use super::{ - deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, hint_code::*, - other_syscalls, syscall_handler::HintProcessorPostRun, + deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, other_syscalls, + syscall_handler::HintProcessorPostRun, }; use crate::{ state::{contract_class_cache::ContractClassCache, state_api::StateReader}, @@ -25,6 +25,31 @@ use cairo_vm::{ }; use std::{any::Any, collections::HashMap}; +#[derive(Clone, Debug)] +pub enum Hint { + Deploy, + EmitEvent, + GetBlockNumber, + GetBlockTimestamp, + GetCallerAddress, + GetSequencerAddress, + LibraryCall, + LibraryCallL1Handler, + CallContract, + StorageRead, + StorageWrite, + SendMessageToL1, + GetTxSignature, + GetTxInfo, + GetContractAddress, + DelegateCall, + DelegateCallL1Handler, + ReplaceClass, + //Normal hint + AddrBoundPrime, + AddrIs250, +} + /// Definition of the deprecated syscall hint processor with associated structs pub(crate) struct DeprecatedSyscallHintProcessor<'a, S: StateReader, C: ContractClassCache> { pub(crate) builtin_hint_processor: BuiltinHintProcessor, @@ -73,6 +98,7 @@ impl<'a, S: StateReader, C: ContractClassCache> DeprecatedSyscallHintProcessor<' constants: &HashMap, ) -> Result<(), SyscallHandlerError> { // Match against specific syscall hint codes and call the appropriate handler + let hint_data = hint_data .downcast_ref::() .ok_or(SyscallHandlerError::WrongHintData)?; @@ -211,7 +237,6 @@ impl<'a, S: StateReader, C: ContractClassCache> HintProcessorLogic SyscallHandlerError::NotImplemented(hint_code) => { HintError::UnknownHint(hint_code.into_boxed_str()) } - e => HintError::CustomHint(e.to_string().into_boxed_str()), })?; } @@ -383,7 +408,11 @@ mod tests { let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_BLOCK_TIMESTAMP.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -417,7 +446,11 @@ mod tests { let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_SEQUENCER_ADDRESS.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -471,7 +504,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(EMIT_EVENT_CODE.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -530,7 +567,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_TX_INFO.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -638,7 +679,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_TX_INFO.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -679,7 +724,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_CALLER_ADDRESS.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -727,7 +776,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(SEND_MESSAGE_TO_L1.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::::default(); @@ -788,8 +841,11 @@ mod tests { RunResources::default(), ); - let hint_data = - HintProcessorData::new_default(GET_BLOCK_NUMBER.to_string(), ids_data!["syscall_ptr"]); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data!["syscall_ptr"], + ); assert_matches!( hint_processor.execute_hint( &mut vm, @@ -815,7 +871,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_CONTRACT_ADDRESS.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::new( @@ -860,7 +920,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(GET_TX_SIGNATURE.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); // invoke syscall let mut state = CachedState::new( @@ -932,7 +996,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(STORAGE_READ.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); let mut state = CachedState::new( Arc::new(InMemoryStateReader::default()), @@ -1000,7 +1068,11 @@ mod tests { // syscall_ptr let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(STORAGE_WRITE.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + ); let mut state = CachedState::new( Arc::new(InMemoryStateReader::default()), @@ -1076,7 +1148,10 @@ mod tests { // Hinta data let ids_data = ids_data!["syscall_ptr"]; - let hint_data = HintProcessorData::new_default(DEPLOY.to_string(), ids_data); + let hint_data = HintProcessorData::new_default( + "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)".to_string(), + ids_data, + ); // Create SyscallHintProcessor let mut state = CachedState::new( diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index de0ca196f..48851e32f 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -2,64 +2,64 @@ // Syscall hints // ************************* -pub(crate) const DEPLOY: &str = - "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; +pub const DEPLOY: &str = "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const EMIT_EVENT_CODE: &str = +pub const EMIT_EVENT_CODE: &str = "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_SEQUENCER_ADDRESS: &str = +pub const GET_SEQUENCER_ADDRESS: &str = "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const STORAGE_WRITE: &str = +pub const STORAGE_WRITE: &str = "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const STORAGE_READ: &str = +pub const STORAGE_READ: &str = "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const SEND_MESSAGE_TO_L1: &str = +pub const SEND_MESSAGE_TO_L1: &str = "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const LIBRARY_CALL_L1_HANDLER: &str = +pub const LIBRARY_CALL_L1_HANDLER: &str = "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const LIBRARY_CALL: &str = +pub const LIBRARY_CALL: &str = "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const CALL_CONTRACT: &str = +pub const CALL_CONTRACT: &str = "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_TX_SIGNATURE: &str = +pub const GET_TX_SIGNATURE: &str = "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_TX_INFO: &str = +pub const GET_TX_INFO: &str = "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_CONTRACT_ADDRESS: &str = +pub const GET_CONTRACT_ADDRESS: &str = "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_CALLER_ADDRESS: &str = +pub const GET_CALLER_ADDRESS: &str = "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_BLOCK_TIMESTAMP: &str = +pub const GET_BLOCK_TIMESTAMP: &str = "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_BLOCK_NUMBER: &str = +pub const GET_BLOCK_NUMBER: &str = "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const DELEGATE_CALL: &str = +pub const DELEGATE_CALL: &str = "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const DELEGATE_L1_HANDLER: &str = +pub const DELEGATE_L1_HANDLER: &str = "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const REPLACE_CLASS: &str = +pub const REPLACE_CLASS: &str = "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; // ************************* // Normal hints // ************************* -pub(crate) const ADDR_BOUND_PRIME: &str = +pub const ADDR_BOUND_PRIME: &str = "# Verify the assumptions on the relationship between 2**250, ADDR_BOUND and PRIME.\nADDR_BOUND = ids.ADDR_BOUND % PRIME\nassert (2**250 < ADDR_BOUND <= 2**251) and (2 * 2**250 < PRIME) and (\n ADDR_BOUND * 2 > PRIME), \\\n 'normalize_address() cannot be used with the current constants.'\nids.is_small = 1 if ids.addr < ADDR_BOUND else 0"; -pub(crate) const ADDR_IS_250: &str = "ids.is_250 = 1 if ids.addr < 2**250 else 0"; + +pub const ADDR_IS_250: &str = "ids.is_250 = 1 if ids.addr < 2**250 else 0"; diff --git a/src/syscalls/hint_code_map.rs b/src/syscalls/hint_code_map.rs new file mode 100644 index 000000000..e59f1b0eb --- /dev/null +++ b/src/syscalls/hint_code_map.rs @@ -0,0 +1,33 @@ +use super::deprecated_syscall_handler::Hint; + +pub static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { + key: 12913932095322966823, + disps: &[ + (1, 0), + (3, 14), + (8, 4), + (7, 18), + ], + entries: &[ + ("syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::ReplaceClass), + ("syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetBlockTimestamp), + ("syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetCallerAddress), + ("syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::LibraryCall), + ("syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::CallContract), + ("syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::DelegateCall), + ("syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetSequencerAddress), + ("syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetTxSignature), + ("syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetBlockNumber), + ("syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::StorageWrite), + ("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::EmitEvent), + ("syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::StorageRead), + ("ids.is_250 = 1 if ids.addr < 2**250 else 0", Hint::AddrIs250), + ("syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::DelegateCallL1Handler), + ("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::Deploy), + ("syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::SendMessageToL1), + ("syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetContractAddress), + ("syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::LibraryCallL1Handler), + ("syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", Hint::GetTxInfo), + ("# Verify the assumptions on the relationship between 2**250, ADDR_BOUND and PRIME.\nADDR_BOUND = ids.ADDR_BOUND % PRIME\nassert (2**250 < ADDR_BOUND <= 2**251) and (2 * 2**250 < PRIME) and (\n ADDR_BOUND * 2 > PRIME), \\\n 'normalize_address() cannot be used with the current constants.'\nids.is_small = 1 if ids.addr < ADDR_BOUND else 0", Hint::AddrBoundPrime), + ], +}; diff --git a/starknet-rs-hint-codegen/Cargo.toml b/starknet-rs-hint-codegen/Cargo.toml new file mode 100644 index 000000000..967d6e856 --- /dev/null +++ b/starknet-rs-hint-codegen/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "starknet-rs-hint-codegen" +version = "0.4.0" +edition = "2021" + +[features] +default = ["with_mimalloc"] +with_mimalloc = ["dep:mimalloc"] + +[dependencies] +starknet_in_rust = { path = "../", version = "0.4.0" } +num-traits = "0.2.15" +serde = { version = "1.0.152", features = ["derive"] } +cairo-vm = { workspace = true, features = ["cairo-1-hints"] } +mimalloc = { version = "0.1.29", default-features = false, optional = true } +codegen = "0.2.0" +phf = { version = "0.11.1", default-features = false } +phf_codegen = "0.11.1" + +[dev-dependencies] +assert_matches = "1.5.0" +coverage-helper = "0.1.0" diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs new file mode 100644 index 000000000..07416529c --- /dev/null +++ b/starknet-rs-hint-codegen/src/main.rs @@ -0,0 +1,39 @@ +use std::fs::File; +use std::io::{BufWriter, Write}; +use std::path::Path; + +use starknet_in_rust::syscalls::hint_code; + +fn main() { + let path = Path::new("src").join("syscalls").join("hint_code_map.rs"); + let mut file = BufWriter::new(File::create(path).unwrap()); + + write!( + &mut file, + "use super::deprecated_syscall_handler::Hint;\n\npub(crate) static HINTCODE: phf::Map<&'static str, Hint> = {}", + phf_codegen::Map::new() + .entry(hint_code::DEPLOY, "Hint::Deploy") + .entry(hint_code::EMIT_EVENT_CODE, "Hint::EmitEvent") + .entry(hint_code::GET_SEQUENCER_ADDRESS, "Hint::GetSequencerAddress") + .entry(hint_code::STORAGE_WRITE, "Hint::StorageWrite") + .entry(hint_code::STORAGE_READ, "Hint::StorageRead") + .entry(hint_code::GET_BLOCK_NUMBER, "Hint::GetBlockNumber") + .entry(hint_code::LIBRARY_CALL, "Hint::LibraryCall") + .entry(hint_code::LIBRARY_CALL_L1_HANDLER, "Hint::LibraryCallL1Handler") + .entry(hint_code::CALL_CONTRACT, "Hint::CallContract") + .entry(hint_code::GET_CALLER_ADDRESS, "Hint::GetCallerAddress") + .entry(hint_code::GET_BLOCK_TIMESTAMP, "Hint::GetBlockTimestamp") + .entry(hint_code::SEND_MESSAGE_TO_L1, "Hint::SendMessageToL1") + .entry(hint_code::GET_TX_SIGNATURE, "Hint::GetTxSignature") + .entry(hint_code::GET_TX_INFO, "Hint::GetTxInfo") + .entry(hint_code::GET_CONTRACT_ADDRESS, "Hint::GetContractAddress") + .entry(hint_code::DELEGATE_CALL, "Hint::DelegateCall") + .entry(hint_code::DELEGATE_L1_HANDLER, "Hint::DelegateCallL1Handler") + .entry(hint_code::REPLACE_CLASS, "Hint::ReplaceClass") + .entry(hint_code::ADDR_BOUND_PRIME, "Hint::AddrBoundPrime") + .entry(hint_code::ADDR_IS_250, "Hint::AddrIs250") + .build() + ) + .unwrap(); + writeln!(&mut file, ";").unwrap(); +}