From 94d1dc61dfe9f3f173798bd3574c7fa326dcd5ff Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 31 Aug 2023 11:48:00 +0300 Subject: [PATCH 01/24] add phf_map problem with the HintProcessorData string --- Cargo.lock | 56 +++++- Cargo.toml | 5 + src/lib.rs | 1 + src/syscalls/deprecated_syscall_handler.rs | 212 +++++++++++++-------- 4 files changed, 193 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 787d2cf04..1d0107832 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2639,6 +2639,48 @@ dependencies = [ "indexmap 1.9.3", ] +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared 0.11.2", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared 0.11.2", + "proc-macro2", + "quote", + "syn 2.0.28", +] + [[package]] name = "phf_shared" version = "0.10.0" @@ -2648,6 +2690,15 @@ dependencies = [ "siphasher", ] +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pico-args" version = "0.5.0" @@ -3624,6 +3675,9 @@ dependencies = [ "num-integer", "num-traits 0.2.16", "once_cell", + "phf", + "phf_codegen", + "phf_macros", "serde", "serde_json", "serde_json_pythonic 0.1.2 (git+https://github.com/xJonathanLEI/serde_json_pythonic?tag=v0.1.2)", @@ -3649,7 +3703,7 @@ dependencies = [ "new_debug_unreachable", "once_cell", "parking_lot 0.12.1", - "phf_shared", + "phf_shared 0.10.0", "precomputed-hash", ] diff --git a/Cargo.toml b/Cargo.toml index 002157c15..ffde854ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,8 @@ starknet = { workspace = true } base64 = { version = "0.21.0", default-features = false, features = ["alloc"] } flate2 = "1.0.25" serde_json_pythonic = { git = "https://github.com/xJonathanLEI/serde_json_pythonic", tag = "v0.1.2"} +phf = "0.11" +phf_macros = "0.11" [dev-dependencies] assert_matches = "1.5.0" @@ -65,3 +67,6 @@ coverage-helper = "0.1.0" path = "bench/internals.rs" name = "internals" harness = false + +[build-dependencies] +phf_codegen = "0.11" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 6dc8c24fa..5ee7ec961 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ use utils::Address; #[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 d131d5ff1..6aff02ac6 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -21,6 +21,54 @@ use cairo_vm::{ vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use std::{any::Any, collections::HashMap}; +use phf_macros::phf_map; + +enum SyscallHandler { + Deploy, + EmitEvent, + GetBlockNumber, + GetBlockTimestamp, + GetCallerAddress, + GetSequencerAddress, + LibraryCall, + LibraryCallL1Handler, + CallContract, + StorageRead, + StorageWrite, + SendMessageToL1, + GetTxSignature, + GetTxInfo, + GetContractAddress, + DelegateCall, + DelegateCallL1Handler, + ReplaceClass, + //Normal hint + AddrBoundPrime, + AddrIs250, +} + +static HINT_CODES: phf::Map<&'static str, SyscallHandler> = phf_map! { + "DEPLOY" => SyscallHandler::Deploy, + "EMIT_EVENT_CODE" => SyscallHandler::EmitEvent, + "GET_BLOCK_NUMBER" => SyscallHandler::GetBlockNumber, + "GET_BLOCK_TIMESTAM" => SyscallHandler::GetBlockTimestamp, + "GET_CALLER_ADDRESS" => SyscallHandler::GetCallerAddress, + "GET_SEQUENCER_ADDRESS" => SyscallHandler::GetSequencerAddress, + "LIBRARY_CALL" => SyscallHandler::LibraryCall, + "LIBRARY_CALL_L1_HANDLER" => SyscallHandler::LibraryCallL1Handler, + "CALL_CONTRACT" => SyscallHandler::CallContract, + "STORAGE_READ" => SyscallHandler::StorageRead, + "STORAGE_WRITE" => SyscallHandler::StorageWrite, + "SEND_MESSAGE_TO_L1" => SyscallHandler::SendMessageToL1, + "GET_TX_SIGNATURE" => SyscallHandler::GetTxSignature, + "GET_TX_INFO" => SyscallHandler::GetTxInfo, + "GET_CONTRACT_ADDRESS" => SyscallHandler::GetContractAddress, + "DELEGATE_CALL" => SyscallHandler::DelegateCall, + "DELEGATE_L1_HANDLER" => SyscallHandler::DelegateCallL1Handler, + "REPLACE_CLASS" => SyscallHandler::ReplaceClass, + "ADDRBOUNDPRIME" => SyscallHandler::AddrBoundPrime, + "ADDRIS250" => SyscallHandler::AddrIs250, +}; /// Definition of the deprecated syscall hint processor with associated structs pub(crate) struct DeprecatedSyscallHintProcessor<'a, S: StateReader> { @@ -71,87 +119,91 @@ impl<'a, S: StateReader> DeprecatedSyscallHintProcessor<'a, S> { ) -> Result<(), SyscallHandlerError> { // Match against specific syscall hint codes and call the appropriate handler let hint_data = hint_data - .downcast_ref::() - .ok_or(SyscallHandlerError::WrongHintData)?; - - match hint_data.code.as_str() { - ADDR_BOUND_PRIME => other_syscalls::addr_bound_prime(vm, hint_data, constants), - ADDR_IS_250 => other_syscalls::addr_is_250(vm, hint_data), - DEPLOY => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.deploy(vm, syscall_ptr) - } - EMIT_EVENT_CODE => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.emit_event(vm, syscall_ptr) - } - GET_BLOCK_NUMBER => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_block_number(vm, syscall_ptr) - } - GET_BLOCK_TIMESTAMP => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_block_timestamp(vm, syscall_ptr) - } - GET_CALLER_ADDRESS => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_caller_address(vm, syscall_ptr) - } - GET_SEQUENCER_ADDRESS => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_sequencer_address(vm, syscall_ptr) - } - LIBRARY_CALL => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.library_call(vm, syscall_ptr) - } - LIBRARY_CALL_L1_HANDLER => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler - .library_call_l1_handler(vm, syscall_ptr) - } - CALL_CONTRACT => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.call_contract(vm, syscall_ptr) - } - STORAGE_READ => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.storage_read(vm, syscall_ptr) - } - STORAGE_WRITE => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.storage_write(vm, syscall_ptr) - } - SEND_MESSAGE_TO_L1 => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.send_message_to_l1(vm, syscall_ptr) - } - GET_TX_SIGNATURE => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_tx_signature(vm, syscall_ptr) - } - GET_TX_INFO => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_tx_info(vm, syscall_ptr) - } - GET_CONTRACT_ADDRESS => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_contract_address(vm, syscall_ptr) - } - DELEGATE_CALL => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.delegate_call(vm, syscall_ptr) - } - DELEGATE_L1_HANDLER => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.delegate_l1_handler(vm, syscall_ptr) - } - REPLACE_CLASS => { - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.replace_class(vm, syscall_ptr) - } - _ => Err(SyscallHandlerError::NotImplemented(hint_data.code.clone())), + .downcast_ref::() + .ok_or(SyscallHandlerError::WrongHintData)?; + + let hint_code = &hint_data.code; + if let Some(syscall) = HINT_CODES.get(hint_code) { + match syscall { + SyscallHandler::AddrBoundPrime => other_syscalls::addr_bound_prime(vm, hint_data, constants), + SyscallHandler::AddrIs250 => other_syscalls::addr_is_250(vm, hint_data), + SyscallHandler::Deploy => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.deploy(vm, syscall_ptr)?; + }), + SyscallHandler::EmitEvent => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.emit_event(vm, syscall_ptr)?; + }), + SyscallHandler::GetBlockNumber => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_block_number(vm, syscall_ptr)?; + }), + SyscallHandler::GetBlockTimestamp => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_block_timestamp(vm, syscall_ptr)?; + }), + SyscallHandler::GetCallerAddress => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_caller_address(vm, syscall_ptr)?; + }), + SyscallHandler::GetSequencerAddress => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_sequencer_address(vm, syscall_ptr)?; + }), + SyscallHandler::LibraryCall => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.library_call(vm, syscall_ptr)?; + }), + SyscallHandler::LibraryCallL1Handler => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.library_call_l1_handler(vm, syscall_ptr)?; + }), + SyscallHandler::CallContract => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.call_contract(vm, syscall_ptr)?; + }), + SyscallHandler::StorageRead => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.storage_read(vm, syscall_ptr)?; + }), + SyscallHandler::StorageWrite => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.storage_write(vm, syscall_ptr)?; + }), + SyscallHandler::SendMessageToL1 => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.send_message_to_l1(vm, syscall_ptr)?; + }), + SyscallHandler::GetTxSignature => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_tx_signature(vm, syscall_ptr)?; + }), + SyscallHandler::GetTxInfo => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_tx_info(vm, syscall_ptr)?; + }), + SyscallHandler::GetContractAddress => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.get_contract_address(vm, syscall_ptr)?; + }), + SyscallHandler::DelegateCall => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.delegate_call(vm, syscall_ptr)?; + }), + SyscallHandler::DelegateCallL1Handler => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.delegate_l1_handler(vm, syscall_ptr)?; + }), + SyscallHandler::ReplaceClass => Ok({ + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + self.syscall_handler.replace_class(vm, syscall_ptr)?; + }), } + } else { + // Make sure to return a Result type here + return Err(SyscallHandlerError::NotImplemented(hint_data.code.clone())); + } } } From 342b8f4f5258616444dd1feda74308017e39c63b Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 31 Aug 2023 11:49:16 +0300 Subject: [PATCH 02/24] add phf_map problem with the HintProcessorData string --- src/syscalls/deprecated_syscall_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 6aff02ac6..6b1653a32 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -1,5 +1,5 @@ use super::{ - deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, hint_code::*, + deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, other_syscalls, syscall_handler::HintProcessorPostRun, }; use crate::{state::state_api::StateReader, syscalls::syscall_handler_errors::SyscallHandlerError}; From 73cffb26fb69e5fa88fd133318f78d91bf477775 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Tue, 5 Sep 2023 15:37:37 +0200 Subject: [PATCH 03/24] wip --- rpc_state_reader/src/lib.rs | 34 ++++- src/syscalls/deprecated_syscall_handler.rs | 158 ++++++++++----------- 2 files changed, 107 insertions(+), 85 deletions(-) diff --git a/rpc_state_reader/src/lib.rs b/rpc_state_reader/src/lib.rs index 254b26b00..552629b33 100644 --- a/rpc_state_reader/src/lib.rs +++ b/rpc_state_reader/src/lib.rs @@ -155,6 +155,9 @@ impl RpcState { .map_err(|err| { RpcError::Cast("Response".to_owned(), "String".to_owned(), err.to_string()) })?; + println!("{}", "Fourth function, I'm reaching this point 1"); + + println!("{}", response.clone()); serde_json::from_str(&response).map_err(|err| RpcError::RpcCall(err.to_string())) } } @@ -329,8 +332,9 @@ impl RpcState { "params": [self.block.to_value()], "id": 1 }); - + println!("{}", "Third function, I'm reaching this point 1"); let block_info: serde_json::Value = self.rpc_call(&get_block_info_params).unwrap(); + println!("{}", "Third function, I'm reaching this point 2"); starknet_in_rust::state::BlockInfo { block_number: block_info["result"]["block_number"] @@ -749,23 +753,37 @@ mod transaction_tests { block_number: u64, gas_price: u128, ) -> TransactionExecutionInfo { + println!("{}", "Second function, I'm reaching this point 1"); + let tx_hash = tx_hash.strip_prefix("0x").unwrap(); + println!("{}", "Second function, I'm reaching this point 2"); // Instantiate the RPC StateReader and the CachedState let block = BlockValue::Number(serde_json::to_value(block_number).unwrap()); + println!("{}", "Second function, I'm reaching this point 3"); + let rpc_state = Arc::new(RpcState::new(network, block)); + println!("{}", "Second function, I'm reaching this point 4"); + let mut state = CachedState::new(rpc_state.clone(), HashMap::new()); + println!("{}", "Second function, I'm reaching this point 5"); let fee_token_address = Address(felt_str!( "049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", 16 )); + println!("{}", "Second function, I'm reaching this point 6"); + let network: StarknetChainId = rpc_state.chain.into(); let starknet_os_config = StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price); + println!("{}", "Second function, I'm reaching this point 7"); + let block_info = rpc_state.get_block_info(starknet_os_config.clone()); + println!("{}", "Second function, I'm reaching this point 8"); + let block_context = BlockContext::new( starknet_os_config, @@ -779,7 +797,11 @@ mod transaction_tests { true, ); + println!("{}", "Second function, I'm reaching this point 9"); + let tx = rpc_state.get_transaction(tx_hash); + println!("{}", "Second function, I'm reaching this point 10"); + tx.execute(&mut state, &block_context, 0).unwrap() } @@ -837,17 +859,27 @@ mod transaction_tests { /// - Link to Explorer: https://testnet.starkscan.co/tx/0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf #[test] fn test_0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf() { + println!("{}", "I'm reaching this point 1"); let result = test_tx( "0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf", RpcChain::TestNet, 838683, 2917470325, ); + println!("{}", "I'm reaching this point 2"); dbg!(&result.actual_resources); + println!("{}", "I'm reaching this point 3"); + dbg!(&result.actual_fee); // test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%) + println!("{}", "I'm reaching this point 4"); + dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer + println!("{}", "I'm reaching this point 5"); + dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer + println!("{}", "I'm reaching this point 6"); + } /// - Transaction Hash: 0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 31c187627..3249396fb 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -23,7 +23,7 @@ use cairo_vm::{ use std::{any::Any, collections::HashMap}; use phf_macros::phf_map; -enum SyscallHandler { +pub enum SyscallHandler { Deploy, EmitEvent, GetBlockNumber, @@ -51,7 +51,7 @@ static HINT_CODES: phf::Map<&'static str, SyscallHandler> = phf_map! { "DEPLOY" => SyscallHandler::Deploy, "EMIT_EVENT_CODE" => SyscallHandler::EmitEvent, "GET_BLOCK_NUMBER" => SyscallHandler::GetBlockNumber, - "GET_BLOCK_TIMESTAM" => SyscallHandler::GetBlockTimestamp, + "GET_BLOCK_TIMESTAMP" => SyscallHandler::GetBlockTimestamp, "GET_CALLER_ADDRESS" => SyscallHandler::GetCallerAddress, "GET_SEQUENCER_ADDRESS" => SyscallHandler::GetSequencerAddress, "LIBRARY_CALL" => SyscallHandler::LibraryCall, @@ -118,92 +118,81 @@ impl<'a, S: StateReader> DeprecatedSyscallHintProcessor<'a, S> { 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)?; let hint_code = &hint_data.code; + let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; if let Some(syscall) = HINT_CODES.get(hint_code) { - match syscall { - SyscallHandler::AddrBoundPrime => other_syscalls::addr_bound_prime(vm, hint_data, constants), - SyscallHandler::AddrIs250 => other_syscalls::addr_is_250(vm, hint_data), - SyscallHandler::Deploy => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.deploy(vm, syscall_ptr)?; - }), - SyscallHandler::EmitEvent => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.emit_event(vm, syscall_ptr)?; - }), - SyscallHandler::GetBlockNumber => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.get_block_number(vm, syscall_ptr)?; - }), - SyscallHandler::GetBlockTimestamp => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + match syscall { + SyscallHandler::AddrBoundPrime => { + other_syscalls::addr_bound_prime(vm, hint_data, constants)?; + } + SyscallHandler::AddrIs250 => { + other_syscalls::addr_is_250(vm, hint_data)?; + } + SyscallHandler::Deploy => { + self.syscall_handler.deploy(vm, syscall_ptr)?; + }, + SyscallHandler::EmitEvent => { + self.syscall_handler.emit_event(vm, syscall_ptr)?; + }, + SyscallHandler::GetBlockNumber => { + self.syscall_handler.get_block_number(vm, syscall_ptr)?; + }, + SyscallHandler::GetBlockTimestamp => { self.syscall_handler.get_block_timestamp(vm, syscall_ptr)?; - }), - SyscallHandler::GetCallerAddress => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::GetCallerAddress => { self.syscall_handler.get_caller_address(vm, syscall_ptr)?; - }), - SyscallHandler::GetSequencerAddress => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::GetSequencerAddress => { self.syscall_handler.get_sequencer_address(vm, syscall_ptr)?; - }), - SyscallHandler::LibraryCall => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::LibraryCall => { self.syscall_handler.library_call(vm, syscall_ptr)?; - }), - SyscallHandler::LibraryCallL1Handler => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::LibraryCallL1Handler => { self.syscall_handler.library_call_l1_handler(vm, syscall_ptr)?; - }), - SyscallHandler::CallContract => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::CallContract => { self.syscall_handler.call_contract(vm, syscall_ptr)?; - }), - SyscallHandler::StorageRead => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::StorageRead => { self.syscall_handler.storage_read(vm, syscall_ptr)?; - }), - SyscallHandler::StorageWrite => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::StorageWrite => { self.syscall_handler.storage_write(vm, syscall_ptr)?; - }), - SyscallHandler::SendMessageToL1 => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::SendMessageToL1 => { self.syscall_handler.send_message_to_l1(vm, syscall_ptr)?; - }), - SyscallHandler::GetTxSignature => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::GetTxSignature => { self.syscall_handler.get_tx_signature(vm, syscall_ptr)?; - }), - SyscallHandler::GetTxInfo => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::GetTxInfo => { self.syscall_handler.get_tx_info(vm, syscall_ptr)?; - }), - SyscallHandler::GetContractAddress => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + }, + SyscallHandler::GetContractAddress => { self.syscall_handler.get_contract_address(vm, syscall_ptr)?; - }), - SyscallHandler::DelegateCall => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.delegate_call(vm, syscall_ptr)?; - }), - SyscallHandler::DelegateCallL1Handler => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.delegate_l1_handler(vm, syscall_ptr)?; - }), - SyscallHandler::ReplaceClass => Ok({ - let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - self.syscall_handler.replace_class(vm, syscall_ptr)?; - }), + }, + SyscallHandler::DelegateCall => { + self.syscall_handler.delegate_call(vm, syscall_ptr)?; + }, + SyscallHandler::DelegateCallL1Handler => { + self.syscall_handler.delegate_l1_handler(vm, syscall_ptr)?; + }, + SyscallHandler::ReplaceClass => { + self.syscall_handler.replace_class(vm, syscall_ptr)?; + }, + }; + Ok(()) + } else { + // Make sure to return a Result type here + Err(SyscallHandlerError::NotImplemented(hint_data.code.clone())) } - } else { - // Make sure to return a Result type here - return Err(SyscallHandlerError::NotImplemented(hint_data.code.clone())); - } } } @@ -223,8 +212,9 @@ impl<'a, S: StateReader> HintProcessorLogic for DeprecatedSyscallHintProcessor<' SyscallHandlerError::NotImplemented(hint_code) => { HintError::UnknownHint(hint_code.into_boxed_str()) } - - e => HintError::CustomHint(e.to_string().into_boxed_str()), + e => { + HintError::CustomHint(e.to_string().into_boxed_str()) + } })?; } Ok(()) @@ -387,7 +377,7 @@ 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("GET_BLOCK_TIMESTAMP".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -421,7 +411,7 @@ 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("GET_SEQUENCER_ADDRESS".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -475,7 +465,7 @@ 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("EMIT_EVENT_CODE".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -534,7 +524,7 @@ 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("GET_TX_INFO".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -642,7 +632,7 @@ 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("GET_TX_INFO".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -683,7 +673,7 @@ 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("GET_CALLER_ADDRESS".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -731,7 +721,7 @@ 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("SEND_MESSAGE_TO_L1".to_string(), ids_data); // invoke syscall let mut state = CachedState::::default(); @@ -790,7 +780,7 @@ mod tests { ); let hint_data = - HintProcessorData::new_default(GET_BLOCK_NUMBER.to_string(), ids_data!["syscall_ptr"]); + HintProcessorData::new_default("GET_BLOCK_NUMBER".to_string(), ids_data!["syscall_ptr"]); assert_matches!( hint_processor.execute_hint( &mut vm, @@ -816,7 +806,7 @@ 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("GET_CONTRACT_ADDRESS".to_string(), ids_data); // invoke syscall let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); @@ -858,7 +848,7 @@ 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("GET_TX_SIGNATURE".to_string(), ids_data); // invoke syscall let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); @@ -927,7 +917,7 @@ 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("STORAGE_READ".to_string(), ids_data); let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( @@ -992,7 +982,7 @@ 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("STORAGE_WRITE".to_string(), ids_data); let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( @@ -1065,7 +1055,7 @@ 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("DEPLOY".to_string(), ids_data); // Create SyscallHintProcessor let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); From 5fabd700237528c06b713a9b7ba292f1a4a45ca2 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Tue, 5 Sep 2023 16:42:40 +0200 Subject: [PATCH 04/24] wip --- rpc_state_reader/src/lib.rs | 44 +++++------------- src/syscalls/hint_code.rs | 90 ++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 79 deletions(-) diff --git a/rpc_state_reader/src/lib.rs b/rpc_state_reader/src/lib.rs index 552629b33..51c4fbe58 100644 --- a/rpc_state_reader/src/lib.rs +++ b/rpc_state_reader/src/lib.rs @@ -155,9 +155,6 @@ impl RpcState { .map_err(|err| { RpcError::Cast("Response".to_owned(), "String".to_owned(), err.to_string()) })?; - println!("{}", "Fourth function, I'm reaching this point 1"); - - println!("{}", response.clone()); serde_json::from_str(&response).map_err(|err| RpcError::RpcCall(err.to_string())) } } @@ -332,9 +329,7 @@ impl RpcState { "params": [self.block.to_value()], "id": 1 }); - println!("{}", "Third function, I'm reaching this point 1"); let block_info: serde_json::Value = self.rpc_call(&get_block_info_params).unwrap(); - println!("{}", "Third function, I'm reaching this point 2"); starknet_in_rust::state::BlockInfo { block_number: block_info["result"]["block_number"] @@ -588,12 +583,9 @@ mod tests { RpcChain::TestNet2, BlockValue::Number(serde_json::to_value(838683).unwrap()), ); - let tx_hash_str = "19feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc"; let tx_hash = felt_str!(format!("{}", tx_hash_str), 16); - let tx_trace = state_reader.get_transaction_trace(tx_hash); - assert_eq!( tx_trace.signature, vec![ @@ -634,6 +626,7 @@ mod tests { felt_str!("38bd34c31a0a5c", 16), ] ); + assert_eq!(tx_trace.validate_invocation.retdata, vec![]); assert_eq!( tx_trace.validate_invocation.execution_resources, @@ -675,6 +668,7 @@ mod tests { felt_str!("38bd34c31a0a5c", 16), ] ); + assert_eq!(tx_trace.function_invocation.retdata, vec![0.into()]); assert_eq!( tx_trace.function_invocation.execution_resources, @@ -687,6 +681,7 @@ mod tests { ]), } ); + assert_eq!(tx_trace.function_invocation.internal_calls.len(), 1); assert_eq!( tx_trace.function_invocation.internal_calls[0] @@ -712,6 +707,7 @@ mod tests { felt_str!("0", 16), ] ); + assert_eq!(tx_trace.fee_transfer_invocation.retdata, vec![1.into()]); assert_eq!( tx_trace.fee_transfer_invocation.execution_resources, @@ -753,37 +749,26 @@ mod transaction_tests { block_number: u64, gas_price: u128, ) -> TransactionExecutionInfo { - println!("{}", "Second function, I'm reaching this point 1"); - let tx_hash = tx_hash.strip_prefix("0x").unwrap(); - println!("{}", "Second function, I'm reaching this point 2"); // Instantiate the RPC StateReader and the CachedState let block = BlockValue::Number(serde_json::to_value(block_number).unwrap()); - println!("{}", "Second function, I'm reaching this point 3"); let rpc_state = Arc::new(RpcState::new(network, block)); - println!("{}", "Second function, I'm reaching this point 4"); let mut state = CachedState::new(rpc_state.clone(), HashMap::new()); - println!("{}", "Second function, I'm reaching this point 5"); let fee_token_address = Address(felt_str!( "049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", 16 )); - println!("{}", "Second function, I'm reaching this point 6"); let network: StarknetChainId = rpc_state.chain.into(); let starknet_os_config = StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price); - println!("{}", "Second function, I'm reaching this point 7"); - let block_info = rpc_state.get_block_info(starknet_os_config.clone()); - println!("{}", "Second function, I'm reaching this point 8"); - let block_context = BlockContext::new( starknet_os_config, @@ -797,11 +782,8 @@ mod transaction_tests { true, ); - println!("{}", "Second function, I'm reaching this point 9"); let tx = rpc_state.get_transaction(tx_hash); - println!("{}", "Second function, I'm reaching this point 10"); - tx.execute(&mut state, &block_context, 0).unwrap() } @@ -859,27 +841,16 @@ mod transaction_tests { /// - Link to Explorer: https://testnet.starkscan.co/tx/0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf #[test] fn test_0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf() { - println!("{}", "I'm reaching this point 1"); let result = test_tx( "0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf", RpcChain::TestNet, 838683, 2917470325, ); - println!("{}", "I'm reaching this point 2"); - dbg!(&result.actual_resources); - println!("{}", "I'm reaching this point 3"); - dbg!(&result.actual_fee); // test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%) - println!("{}", "I'm reaching this point 4"); - dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer - println!("{}", "I'm reaching this point 5"); - dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer - println!("{}", "I'm reaching this point 6"); - } /// - Transaction Hash: 0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc @@ -933,17 +904,22 @@ mod transaction_tests { /// - Link to explorer: https://starkscan.co/tx/0x026a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c #[test] fn test_0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c() { + println!("{}", "work 0"); let result = test_tx( "0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c", RpcChain::MainNet, 155054, 33977120598, ); - + println!("{}", "work 1"); dbg!(&result.actual_resources); + println!("{}", "work 2"); dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%) + println!("{}", "work 3"); dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer + println!("{}", "work 4"); dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer + println!("{}", "work 5"); } // Fails because there is a problem with get_compiled_class_hash diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index de0ca196f..2b46ab11f 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -1,65 +1,65 @@ -// ************************* -// Syscall hints -// ************************* +// // ************************* +// // Syscall hints +// // ************************* -pub(crate) const DEPLOY: &str = - "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const DEPLOY: &str = +// "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const EMIT_EVENT_CODE: &str = - "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const EMIT_EVENT_CODE: &str = +// "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_SEQUENCER_ADDRESS: &str = - "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_SEQUENCER_ADDRESS: &str = +// "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const STORAGE_WRITE: &str = - "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const STORAGE_WRITE: &str = +// "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const STORAGE_READ: &str = - "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const STORAGE_READ: &str = +// "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const SEND_MESSAGE_TO_L1: &str = - "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) 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 = - "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) 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 = - "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const LIBRARY_CALL: &str = +// "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const CALL_CONTRACT: &str = - "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const CALL_CONTRACT: &str = +// "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_TX_SIGNATURE: &str = - "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_TX_SIGNATURE: &str = +// "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_TX_INFO: &str = - "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_TX_INFO: &str = +// "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_CONTRACT_ADDRESS: &str = - "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_CONTRACT_ADDRESS: &str = +// "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_CALLER_ADDRESS: &str = - "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_CALLER_ADDRESS: &str = +// "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_BLOCK_TIMESTAMP: &str = - "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_BLOCK_TIMESTAMP: &str = +// "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const GET_BLOCK_NUMBER: &str = - "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const GET_BLOCK_NUMBER: &str = +// "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const DELEGATE_CALL: &str = - "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const DELEGATE_CALL: &str = +// "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const DELEGATE_L1_HANDLER: &str = - "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const DELEGATE_L1_HANDLER: &str = +// "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub(crate) const REPLACE_CLASS: &str = - "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// pub(crate) const REPLACE_CLASS: &str = +// "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// ************************* -// Normal hints -// ************************* +// // ************************* +// // Normal hints +// // ************************* -pub(crate) 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(crate) 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"; From c4bfe789a21a3a67c60f423eae5cda36905857e6 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 7 Sep 2023 13:32:28 +0200 Subject: [PATCH 05/24] created perfect hashing mapping intead of strings --- rpc_state_reader/src/lib.rs | 8 - src/syscalls/deprecated_syscall_handler.rs | 264 ++++++++++++--------- 2 files changed, 157 insertions(+), 115 deletions(-) diff --git a/rpc_state_reader/src/lib.rs b/rpc_state_reader/src/lib.rs index a7ed437b0..8f7c6cecd 100644 --- a/rpc_state_reader/src/lib.rs +++ b/rpc_state_reader/src/lib.rs @@ -763,7 +763,6 @@ mod transaction_tests { 16 )); - let network: StarknetChainId = rpc_state.chain.into(); let starknet_os_config = StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price); @@ -782,7 +781,6 @@ mod transaction_tests { true, ); - let tx = rpc_state.get_transaction(tx_hash); tx.execute(&mut state, &block_context, 0).unwrap() @@ -904,22 +902,16 @@ mod transaction_tests { /// - Link to explorer: https://starkscan.co/tx/0x026a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c #[test] fn test_0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c() { - println!("{}", "work 0"); let result = test_tx( "0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c", RpcChain::MainNet, 155054, 33977120598, ); - println!("{}", "work 1"); dbg!(&result.actual_resources); - println!("{}", "work 2"); dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%) - println!("{}", "work 3"); dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer - println!("{}", "work 4"); dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer - println!("{}", "work 5"); } // Fails because there is a problem with get_compiled_class_hash diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 3249396fb..7fab3c354 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, - other_syscalls, syscall_handler::HintProcessorPostRun, + deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler, other_syscalls, + syscall_handler::HintProcessorPostRun, }; use crate::{state::state_api::StateReader, syscalls::syscall_handler_errors::SyscallHandlerError}; use cairo_vm::{ @@ -20,10 +20,10 @@ use cairo_vm::{ types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; -use std::{any::Any, collections::HashMap}; use phf_macros::phf_map; +use std::{any::Any, collections::HashMap}; -pub enum SyscallHandler { +pub enum Hint { Deploy, EmitEvent, GetBlockNumber, @@ -47,27 +47,27 @@ pub enum SyscallHandler { AddrIs250, } -static HINT_CODES: phf::Map<&'static str, SyscallHandler> = phf_map! { - "DEPLOY" => SyscallHandler::Deploy, - "EMIT_EVENT_CODE" => SyscallHandler::EmitEvent, - "GET_BLOCK_NUMBER" => SyscallHandler::GetBlockNumber, - "GET_BLOCK_TIMESTAMP" => SyscallHandler::GetBlockTimestamp, - "GET_CALLER_ADDRESS" => SyscallHandler::GetCallerAddress, - "GET_SEQUENCER_ADDRESS" => SyscallHandler::GetSequencerAddress, - "LIBRARY_CALL" => SyscallHandler::LibraryCall, - "LIBRARY_CALL_L1_HANDLER" => SyscallHandler::LibraryCallL1Handler, - "CALL_CONTRACT" => SyscallHandler::CallContract, - "STORAGE_READ" => SyscallHandler::StorageRead, - "STORAGE_WRITE" => SyscallHandler::StorageWrite, - "SEND_MESSAGE_TO_L1" => SyscallHandler::SendMessageToL1, - "GET_TX_SIGNATURE" => SyscallHandler::GetTxSignature, - "GET_TX_INFO" => SyscallHandler::GetTxInfo, - "GET_CONTRACT_ADDRESS" => SyscallHandler::GetContractAddress, - "DELEGATE_CALL" => SyscallHandler::DelegateCall, - "DELEGATE_L1_HANDLER" => SyscallHandler::DelegateCallL1Handler, - "REPLACE_CLASS" => SyscallHandler::ReplaceClass, - "ADDRBOUNDPRIME" => SyscallHandler::AddrBoundPrime, - "ADDRIS250" => SyscallHandler::AddrIs250, +static HINT_CODES: phf::Map<&'static str, Hint> = phf_map! { + "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::Deploy, + "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::EmitEvent, + "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetSequencerAddress, + "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::StorageWrite, + "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::StorageRead, + "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetBlockNumber, + "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::LibraryCall, + "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::LibraryCallL1Handler, + "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::CallContract, + "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetCallerAddress, + "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetBlockTimestamp, + "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::SendMessageToL1, + "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetTxSignature, + "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetTxInfo, + "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetContractAddress, + "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::DelegateCall, + "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::DelegateCallL1Handler, + "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::ReplaceClass, + "# 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, + "ids.is_250 = 1 if ids.addr < 2**250 else 0" => Hint::AddrIs250, }; /// Definition of the deprecated syscall hint processor with associated structs @@ -120,75 +120,77 @@ impl<'a, S: StateReader> DeprecatedSyscallHintProcessor<'a, S> { // Match against specific syscall hint codes and call the appropriate handler let hint_data = hint_data - .downcast_ref::() - .ok_or(SyscallHandlerError::WrongHintData)?; + .downcast_ref::() + .ok_or(SyscallHandlerError::WrongHintData)?; let hint_code = &hint_data.code; let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; if let Some(syscall) = HINT_CODES.get(hint_code) { - match syscall { - SyscallHandler::AddrBoundPrime => { - other_syscalls::addr_bound_prime(vm, hint_data, constants)?; - } - SyscallHandler::AddrIs250 => { - other_syscalls::addr_is_250(vm, hint_data)?; - } - SyscallHandler::Deploy => { - self.syscall_handler.deploy(vm, syscall_ptr)?; - }, - SyscallHandler::EmitEvent => { - self.syscall_handler.emit_event(vm, syscall_ptr)?; - }, - SyscallHandler::GetBlockNumber => { - self.syscall_handler.get_block_number(vm, syscall_ptr)?; - }, - SyscallHandler::GetBlockTimestamp => { - self.syscall_handler.get_block_timestamp(vm, syscall_ptr)?; - }, - SyscallHandler::GetCallerAddress => { - self.syscall_handler.get_caller_address(vm, syscall_ptr)?; - }, - SyscallHandler::GetSequencerAddress => { - self.syscall_handler.get_sequencer_address(vm, syscall_ptr)?; - }, - SyscallHandler::LibraryCall => { - self.syscall_handler.library_call(vm, syscall_ptr)?; - }, - SyscallHandler::LibraryCallL1Handler => { - self.syscall_handler.library_call_l1_handler(vm, syscall_ptr)?; - }, - SyscallHandler::CallContract => { - self.syscall_handler.call_contract(vm, syscall_ptr)?; - }, - SyscallHandler::StorageRead => { - self.syscall_handler.storage_read(vm, syscall_ptr)?; - }, - SyscallHandler::StorageWrite => { - self.syscall_handler.storage_write(vm, syscall_ptr)?; - }, - SyscallHandler::SendMessageToL1 => { - self.syscall_handler.send_message_to_l1(vm, syscall_ptr)?; - }, - SyscallHandler::GetTxSignature => { - self.syscall_handler.get_tx_signature(vm, syscall_ptr)?; - }, - SyscallHandler::GetTxInfo => { - self.syscall_handler.get_tx_info(vm, syscall_ptr)?; - }, - SyscallHandler::GetContractAddress => { - self.syscall_handler.get_contract_address(vm, syscall_ptr)?; - }, - SyscallHandler::DelegateCall => { - self.syscall_handler.delegate_call(vm, syscall_ptr)?; - }, - SyscallHandler::DelegateCallL1Handler => { - self.syscall_handler.delegate_l1_handler(vm, syscall_ptr)?; - }, - SyscallHandler::ReplaceClass => { - self.syscall_handler.replace_class(vm, syscall_ptr)?; - }, - }; - Ok(()) + match syscall { + Hint::AddrBoundPrime => { + other_syscalls::addr_bound_prime(vm, hint_data, constants)?; + } + Hint::AddrIs250 => { + other_syscalls::addr_is_250(vm, hint_data)?; + } + Hint::Deploy => { + self.syscall_handler.deploy(vm, syscall_ptr)?; + } + Hint::EmitEvent => { + self.syscall_handler.emit_event(vm, syscall_ptr)?; + } + Hint::GetBlockNumber => { + self.syscall_handler.get_block_number(vm, syscall_ptr)?; + } + Hint::GetBlockTimestamp => { + self.syscall_handler.get_block_timestamp(vm, syscall_ptr)?; + } + Hint::GetCallerAddress => { + self.syscall_handler.get_caller_address(vm, syscall_ptr)?; + } + Hint::GetSequencerAddress => { + self.syscall_handler + .get_sequencer_address(vm, syscall_ptr)?; + } + Hint::LibraryCall => { + self.syscall_handler.library_call(vm, syscall_ptr)?; + } + Hint::LibraryCallL1Handler => { + self.syscall_handler + .library_call_l1_handler(vm, syscall_ptr)?; + } + Hint::CallContract => { + self.syscall_handler.call_contract(vm, syscall_ptr)?; + } + Hint::StorageRead => { + self.syscall_handler.storage_read(vm, syscall_ptr)?; + } + Hint::StorageWrite => { + self.syscall_handler.storage_write(vm, syscall_ptr)?; + } + Hint::SendMessageToL1 => { + self.syscall_handler.send_message_to_l1(vm, syscall_ptr)?; + } + Hint::GetTxSignature => { + self.syscall_handler.get_tx_signature(vm, syscall_ptr)?; + } + Hint::GetTxInfo => { + self.syscall_handler.get_tx_info(vm, syscall_ptr)?; + } + Hint::GetContractAddress => { + self.syscall_handler.get_contract_address(vm, syscall_ptr)?; + } + Hint::DelegateCall => { + self.syscall_handler.delegate_call(vm, syscall_ptr)?; + } + Hint::DelegateCallL1Handler => { + self.syscall_handler.delegate_l1_handler(vm, syscall_ptr)?; + } + Hint::ReplaceClass => { + self.syscall_handler.replace_class(vm, syscall_ptr)?; + } + }; + Ok(()) } else { // Make sure to return a Result type here Err(SyscallHandlerError::NotImplemented(hint_data.code.clone())) @@ -212,9 +214,7 @@ impl<'a, S: StateReader> HintProcessorLogic for DeprecatedSyscallHintProcessor<' SyscallHandlerError::NotImplemented(hint_code) => { HintError::UnknownHint(hint_code.into_boxed_str()) } - e => { - HintError::CustomHint(e.to_string().into_boxed_str()) - } + e => HintError::CustomHint(e.to_string().into_boxed_str()), })?; } Ok(()) @@ -377,7 +377,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(); @@ -411,7 +415,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(); @@ -465,7 +473,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(); @@ -524,7 +536,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(); @@ -632,7 +648,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(); @@ -673,7 +693,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(); @@ -721,7 +745,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(); @@ -779,8 +807,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, @@ -806,7 +837,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(Arc::new(InMemoryStateReader::default()), HashMap::new()); @@ -848,7 +883,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(Arc::new(InMemoryStateReader::default()), HashMap::new()); @@ -917,7 +956,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()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( @@ -982,7 +1025,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()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( @@ -1055,7 +1102,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(Arc::new(InMemoryStateReader::default()), HashMap::new()); From efee61ecc7cb77898b0ef52180408b0b9025a4d4 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 7 Sep 2023 13:36:17 +0200 Subject: [PATCH 06/24] Removed hint_code.rs --- src/syscalls/hint_code.rs | 65 --------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 src/syscalls/hint_code.rs diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs deleted file mode 100644 index 2b46ab11f..000000000 --- a/src/syscalls/hint_code.rs +++ /dev/null @@ -1,65 +0,0 @@ -// // ************************* -// // Syscall hints -// // ************************* - -// pub(crate) const DEPLOY: &str = -// "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const EMIT_EVENT_CODE: &str = -// "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_SEQUENCER_ADDRESS: &str = -// "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const STORAGE_WRITE: &str = -// "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const STORAGE_READ: &str = -// "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) 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 = -// "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const LIBRARY_CALL: &str = -// "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const CALL_CONTRACT: &str = -// "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_TX_SIGNATURE: &str = -// "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_TX_INFO: &str = -// "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_CONTRACT_ADDRESS: &str = -// "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_CALLER_ADDRESS: &str = -// "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_BLOCK_TIMESTAMP: &str = -// "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_BLOCK_NUMBER: &str = -// "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const DELEGATE_CALL: &str = -// "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const DELEGATE_L1_HANDLER: &str = -// "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const REPLACE_CLASS: &str = -// "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // ************************* -// // Normal hints -// // ************************* - -// pub(crate) 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"; From 9bc956a7d7faf6d2cde86795dfe8be3161ea159a Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 7 Sep 2023 13:55:54 +0200 Subject: [PATCH 07/24] remove hint_code from mod.rs --- src/syscalls/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 065e31652..843789e09 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -3,7 +3,6 @@ pub mod deprecated_business_logic_syscall_handler; pub mod deprecated_syscall_handler; pub mod deprecated_syscall_request; pub mod deprecated_syscall_response; -pub mod hint_code; pub mod other_syscalls; pub mod syscall_handler; pub mod syscall_handler_errors; From 5b7cfe9879cc8b3662b7b31795757211f362903d Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Mon, 11 Sep 2023 13:58:20 +0200 Subject: [PATCH 08/24] wip --- Cargo.lock | 282 +++++++++++++++++++++++++++++++++- Cargo.toml | 5 +- bench/hashing_running_time.rs | 33 ++++ 3 files changed, 315 insertions(+), 5 deletions(-) create mode 100644 bench/hashing_running_time.rs diff --git a/Cargo.lock b/Cargo.lock index ceb570d8d..4e712630a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "0.5.0" @@ -515,6 +521,17 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "auto_impl" version = "1.1.0" @@ -1290,6 +1307,12 @@ dependencies = [ "thiserror-no-std", ] +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cc" version = "1.0.83" @@ -1319,6 +1342,33 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "ciborium" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" + +[[package]] +name = "ciborium-ll" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "cipher" version = "0.4.4" @@ -1329,6 +1379,18 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "bitflags 1.3.2", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "textwrap", +] + [[package]] name = "clap" version = "4.4.2" @@ -1347,7 +1409,7 @@ checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ "anstream", "anstyle", - "clap_lex", + "clap_lex 0.5.1", "strsim", ] @@ -1363,6 +1425,15 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clap_lex" version = "0.5.1" @@ -1448,6 +1519,76 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "criterion" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +dependencies = [ + "anes", + "atty", + "cast", + "ciborium", + "clap 3.2.25", + "criterion-plot", + "itertools 0.10.5", + "lazy_static", + "num-traits 0.2.16", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools 0.10.5", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + [[package]] name = "crossbeam-utils" version = "0.8.16" @@ -2002,6 +2143,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + [[package]] name = "hashbrown" version = "0.12.3" @@ -2046,6 +2193,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.2" @@ -2313,7 +2469,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.2", "rustix", "windows-sys", ] @@ -2505,6 +2661,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "mimalloc" version = "0.1.38" @@ -2664,6 +2829,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi 0.3.2", + "libc", +] + [[package]] name = "object" version = "0.32.0" @@ -2694,6 +2869,12 @@ dependencies = [ "num-traits 0.2.16", ] +[[package]] +name = "os_str_bytes" +version = "6.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" + [[package]] name = "parity-scale-codec" version = "3.6.4" @@ -2890,6 +3071,34 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "plotters" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" +dependencies = [ + "num-traits 0.2.16", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609" + +[[package]] +name = "plotters-svg" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab" +dependencies = [ + "plotters-backend", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -3029,6 +3238,28 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -3337,6 +3568,15 @@ dependencies = [ "cipher", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schemars" version = "0.8.13" @@ -3829,7 +4069,7 @@ dependencies = [ "assert_matches", "awc", "cairo-vm", - "clap", + "clap 4.4.2", "coverage-helper", "mimalloc", "num-traits 0.2.16", @@ -3885,6 +4125,7 @@ dependencies = [ "cairo-lang-utils", "cairo-vm", "coverage-helper", + "criterion", "flate2", "getset", "hex", @@ -4045,6 +4286,12 @@ dependencies = [ "test-case-core", ] +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "thiserror" version = "1.0.47" @@ -4122,6 +4369,16 @@ dependencies = [ "crunchy", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -4380,6 +4637,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -4502,6 +4769,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index ed427e441..c9b7823b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,10 +62,11 @@ phf_macros = "0.11" [dev-dependencies] assert_matches = "1.5.0" coverage-helper = "0.1.0" +criterion = "0.4" [[bench]] -path = "bench/internals.rs" -name = "internals" +path = "bench/hashing_running_time.rs" +name = "hashing_running_time" harness = false [build-dependencies] diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs new file mode 100644 index 000000000..cbde7e720 --- /dev/null +++ b/bench/hashing_running_time.rs @@ -0,0 +1,33 @@ +#![deny(warnings)] + +use starknet_in_rust::syscalls::deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler; +use cairo_vm::vm; +use cairo_vm::vm::runners::cairo_runner::RunResources; +use std::collections::HashMap; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use cairo_vm::types::exec_scope::ExecutionScopes; +use std::{any::Any, collections::HashMap}; + + +fn criterion_benchmark(c: &mut Criterion) { + // Prepare any required setup here. This might include setting up a VirtualMachine, ExecutionScopes, etc. + let mut vm = vm!(); + let mut exec_scopes = &mut ExecutionScopes::new(); + let hint_data = &any_box!(hint_data); + let constants = &HashMap::new(); + + // Benchmark the execute_hint method + c.bench_function("execute_hint", |b| b.iter(|| { + let mut state = CachedState::::default(); + let mut syscall_handler = DeprecatedSyscallHintProcessor::new( + DeprecatedBLSyscallHandler::default_with(&mut state), + RunResources::default(), + ); + black_box( + syscall_handler.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants) + ); + })); +} + +criterion_group!(bench, criterion_benchmark); +criterion_main!(bench); From e04efca236a4b4e259e1f0aa08d45936f7bb7637 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 14 Sep 2023 12:28:38 +0300 Subject: [PATCH 09/24] wip --- Cargo.lock | 115 ++++++++++++++++------------------ Cargo.toml | 7 ++- bench/hashing_running_time.rs | 37 +++++------ 3 files changed, 74 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e712630a..2eec03b08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,12 +295,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anes" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" - [[package]] name = "anstream" version = "0.5.0" @@ -1342,33 +1336,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "ciborium" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" -dependencies = [ - "ciborium-io", - "ciborium-ll", - "serde", -] - -[[package]] -name = "ciborium-io" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" - -[[package]] -name = "ciborium-ll" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" -dependencies = [ - "ciborium-io", - "half", -] - [[package]] name = "cipher" version = "0.4.4" @@ -1381,14 +1348,13 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.25" +version = "2.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "bitflags 1.3.2", - "clap_lex 0.2.4", - "indexmap 1.9.3", "textwrap", + "unicode-width", ] [[package]] @@ -1409,7 +1375,7 @@ checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ "anstream", "anstyle", - "clap_lex 0.5.1", + "clap_lex", "strsim", ] @@ -1425,15 +1391,6 @@ dependencies = [ "syn 2.0.29", ] -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", -] - [[package]] name = "clap_lex" version = "0.5.1" @@ -1521,16 +1478,15 @@ dependencies = [ [[package]] name = "criterion" -version = "0.4.0" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" dependencies = [ - "anes", "atty", "cast", - "ciborium", - "clap 3.2.25", + "clap 2.34.0", "criterion-plot", + "csv", "itertools 0.10.5", "lazy_static", "num-traits 0.2.16", @@ -1539,6 +1495,7 @@ dependencies = [ "rayon", "regex", "serde", + "serde_cbor", "serde_derive", "serde_json", "tinytemplate", @@ -1547,9 +1504,9 @@ dependencies = [ [[package]] name = "criterion-plot" -version = "0.5.0" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" dependencies = [ "cast", "itertools 0.10.5", @@ -1625,6 +1582,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "ctor" version = "0.2.4" @@ -2869,12 +2847,6 @@ dependencies = [ "num-traits 0.2.16", ] -[[package]] -name = "os_str_bytes" -version = "6.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" - [[package]] name = "parity-scale-codec" version = "3.6.4" @@ -3645,6 +3617,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_cbor" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" +dependencies = [ + "half", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.171" @@ -4288,9 +4270,12 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.16.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] [[package]] name = "thiserror" @@ -4568,6 +4553,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.4" diff --git a/Cargo.toml b/Cargo.toml index c9b7823b8..dccae74d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,12 +62,17 @@ phf_macros = "0.11" [dev-dependencies] assert_matches = "1.5.0" coverage-helper = "0.1.0" -criterion = "0.4" +criterion = "0.3" [[bench]] path = "bench/hashing_running_time.rs" name = "hashing_running_time" harness = false +[[bench]] +path = "bench/internals.rs" +name = "internals" +harness = false + [build-dependencies] phf_codegen = "0.11" \ No newline at end of file diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index cbde7e720..6193ddb7a 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -1,31 +1,24 @@ -#![deny(warnings)] - -use starknet_in_rust::syscalls::deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler; -use cairo_vm::vm; -use cairo_vm::vm::runners::cairo_runner::RunResources; -use std::collections::HashMap; -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use cairo_vm::types::exec_scope::ExecutionScopes; -use std::{any::Any, collections::HashMap}; +#[deny(unused_imports)] +use criterion::{criterion_group, criterion_main, Criterion}; +use cairo_vm::vm::vm_core::VirtualMachine; fn criterion_benchmark(c: &mut Criterion) { - // Prepare any required setup here. This might include setting up a VirtualMachine, ExecutionScopes, etc. - let mut vm = vm!(); - let mut exec_scopes = &mut ExecutionScopes::new(); - let hint_data = &any_box!(hint_data); - let constants = &HashMap::new(); + let mut vm = VirtualMachine::new(false); + // let mut exec_scopes = &mut ExecutionScopes::new(); + // let hint_data = &any_box!(hint_data); + // let constants = &HashMap::new(); // Benchmark the execute_hint method c.bench_function("execute_hint", |b| b.iter(|| { - let mut state = CachedState::::default(); - let mut syscall_handler = DeprecatedSyscallHintProcessor::new( - DeprecatedBLSyscallHandler::default_with(&mut state), - RunResources::default(), - ); - black_box( - syscall_handler.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants) - ); + // let mut state = CachedState::::default(); + // let mut syscall_handler = DeprecatedSyscallHintProcessor::new( + // DeprecatedBLSyscallHandler::default_with(&mut state), + // RunResources::default(), + // ); + // black_box( + // syscall_handler.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants) + // ); })); } From 3c9bdbee48ac3ae1b90d241b69c7d6151f124540 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 14 Sep 2023 19:25:00 +0300 Subject: [PATCH 10/24] wip --- Cargo.lock | 16 +++++- bench/hashing_running_time.rs | 62 ++++++++++++++++----- rpc_state_reader_sn_api/Cargo.toml | 29 ---------- src/syscalls/deprecated_syscall_handler.rs | 2 +- src/syscalls/hint_code.rs | 65 ++++++++++++++++++++++ src/syscalls/mod.rs | 1 + 6 files changed, 128 insertions(+), 47 deletions(-) delete mode 100644 rpc_state_reader_sn_api/Cargo.toml create mode 100644 src/syscalls/hint_code.rs diff --git a/Cargo.lock b/Cargo.lock index 70522a2b8..44c036f70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3654,6 +3654,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_json_pythonic" +version = "0.1.2" +source = "git+https://github.com/xJonathanLEI/serde_json_pythonic?tag=v0.1.2#4e4b08bab95309be8672b4efd346bea900040e15" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.3" @@ -3895,7 +3905,7 @@ dependencies = [ "hex", "serde", "serde_json", - "serde_json_pythonic", + "serde_json_pythonic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde_with 2.3.3", "sha3", "starknet-crypto 0.6.0", @@ -4091,13 +4101,13 @@ dependencies = [ "num-integer", "num-traits 0.2.16", "once_cell", - "pretty_assertions_sorted", "phf", "phf_codegen", "phf_macros", + "pretty_assertions_sorted", "serde", "serde_json", - "serde_json_pythonic", + "serde_json_pythonic 0.1.2 (git+https://github.com/xJonathanLEI/serde_json_pythonic?tag=v0.1.2)", "sha3", "starknet", "starknet-crypto 0.5.1", diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index 6193ddb7a..f260b632e 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -1,24 +1,58 @@ +use std::collections::HashMap; + #[deny(unused_imports)] use criterion::{criterion_group, criterion_main, Criterion}; -use cairo_vm::vm::vm_core::VirtualMachine; - +use cairo_vm::{vm::{vm_core::VirtualMachine, runners::cairo_runner::RunResources}, types::exec_scope::ExecutionScopes, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData}; +use starknet_in_rust::{state::{cached_state::CachedState, in_memory_state_reader::InMemoryStateReader}, syscalls::deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler}; +use starknet_in_rust::syscalls::deprecated_syscall_handler::DeprecatedSyscallHintProcessor; +use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic; +use std::{hint::black_box, any::Any}; fn criterion_benchmark(c: &mut Criterion) { - let mut vm = VirtualMachine::new(false); - // let mut exec_scopes = &mut ExecutionScopes::new(); - // let hint_data = &any_box!(hint_data); - // let constants = &HashMap::new(); + let mut 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 as i32, + ), + ); + } + references + }; + + let mut ids_data = HashMap::::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( + "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" + .to_string(), + ids_data, + )); + // Benchmark the execute_hint method c.bench_function("execute_hint", |b| b.iter(|| { - // let mut state = CachedState::::default(); - // let mut syscall_handler = DeprecatedSyscallHintProcessor::new( - // DeprecatedBLSyscallHandler::default_with(&mut state), - // RunResources::default(), - // ); - // black_box( - // syscall_handler.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants) - // ); + 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, &mut exec_scopes, &hint_data, &constants) + ); })); } diff --git a/rpc_state_reader_sn_api/Cargo.toml b/rpc_state_reader_sn_api/Cargo.toml deleted file mode 100644 index 35be6617f..000000000 --- a/rpc_state_reader_sn_api/Cargo.toml +++ /dev/null @@ -1,29 +0,0 @@ -[package] -name = "rpc_state_reader_sn_api" -version = "0.3.1" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -ureq = { version = "2.7.1", features = ["json"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = { version = "1.0", features = [ - "arbitrary_precision", - "raw_value", -] } -starknet_api = "0.4.1" -cairo-lang-starknet = { workspace = true } -cairo-lang-utils = { workspace = true } -starknet = { workspace = true } -thiserror = { workspace = true } -flate2 = "1.0.25" -serde_with = "3.0.0" -dotenv = "0.15.0" -cairo-vm = "0.8.5" -blockifier = "0.2.0-rc0" -starknet_in_rust = { path = "../", version = "0.3.1" } - -[dev-dependencies] -pretty_assertions_sorted = "1.2.3" -test-case = "3.1.0" diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 61a6c544d..649c06690 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -71,7 +71,7 @@ static HINT_CODES: phf::Map<&'static str, Hint> = phf_map! { }; /// Definition of the deprecated syscall hint processor with associated structs -pub(crate) struct DeprecatedSyscallHintProcessor<'a, S: StateReader> { +pub struct DeprecatedSyscallHintProcessor<'a, S: StateReader> { pub(crate) builtin_hint_processor: BuiltinHintProcessor, pub(crate) syscall_handler: DeprecatedBLSyscallHandler<'a, S>, run_resources: RunResources, diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs new file mode 100644 index 000000000..2b46ab11f --- /dev/null +++ b/src/syscalls/hint_code.rs @@ -0,0 +1,65 @@ +// // ************************* +// // Syscall hints +// // ************************* + +// pub(crate) const DEPLOY: &str = +// "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const EMIT_EVENT_CODE: &str = +// "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_SEQUENCER_ADDRESS: &str = +// "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const STORAGE_WRITE: &str = +// "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const STORAGE_READ: &str = +// "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) 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 = +// "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const LIBRARY_CALL: &str = +// "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const CALL_CONTRACT: &str = +// "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_TX_SIGNATURE: &str = +// "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_TX_INFO: &str = +// "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_CONTRACT_ADDRESS: &str = +// "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_CALLER_ADDRESS: &str = +// "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_BLOCK_TIMESTAMP: &str = +// "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const GET_BLOCK_NUMBER: &str = +// "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const DELEGATE_CALL: &str = +// "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const DELEGATE_L1_HANDLER: &str = +// "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// pub(crate) const REPLACE_CLASS: &str = +// "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// // ************************* +// // Normal hints +// // ************************* + +// pub(crate) 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"; diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 843789e09..6ffb0b686 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -9,3 +9,4 @@ pub mod syscall_handler_errors; pub mod syscall_info; pub mod syscall_request; pub mod syscall_response; +//pub mod hint_code; From 39e1e8c27293fb3c02e7d3d425fa8c580939198b Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Fri, 15 Sep 2023 14:29:48 +0300 Subject: [PATCH 11/24] added benchmarking --- bench/hashing_running_time.rs | 58 ++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index f260b632e..51cd99424 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -1,15 +1,21 @@ -use std::collections::HashMap; - +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}, +}; #[deny(unused_imports)] use criterion::{criterion_group, criterion_main, Criterion}; -use cairo_vm::{vm::{vm_core::VirtualMachine, runners::cairo_runner::RunResources}, types::exec_scope::ExecutionScopes, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData}; -use starknet_in_rust::{state::{cached_state::CachedState, in_memory_state_reader::InMemoryStateReader}, syscalls::deprecated_business_logic_syscall_handler::DeprecatedBLSyscallHandler}; use starknet_in_rust::syscalls::deprecated_syscall_handler::DeprecatedSyscallHintProcessor; -use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic; -use std::{hint::black_box, any::Any}; +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 mut exec_scopes = &mut ExecutionScopes::new(); + let exec_scopes = &mut ExecutionScopes::new(); let ids_names = vec!["syscall_ptr"]; let references = { let mut references = HashMap::< @@ -20,40 +26,44 @@ fn criterion_benchmark(c: &mut Criterion) { references.insert( i as usize, cairo_vm::hint_processor::hint_processor_definition::HintReference::new_simple( - i as i32, + i, ), ); } references }; - let mut ids_data = HashMap::::new(); + let mut ids_data = + HashMap::::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( "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" .to_string(), ids_data, )); - // Benchmark the execute_hint method - c.bench_function("execute_hint", |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, &mut exec_scopes, &hint_data, &constants) - ); - })); + c.bench_function("execute_hint", |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); From 61712352e2d4274755385cbd66548548f86d38ac Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Mon, 18 Sep 2023 11:28:33 +0300 Subject: [PATCH 12/24] format --- bench/hashing_running_time.rs | 6 ++---- bench/internals.rs | 2 ++ src/syscalls/hint_code.rs | 38 +++++++++++++++++------------------ src/syscalls/mod.rs | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index 51cd99424..a32e57418 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -13,7 +13,7 @@ use starknet_in_rust::{ }; use std::collections::HashMap; use std::{any::Any, hint::black_box}; - +#[allow(dead_code)] fn criterion_benchmark(c: &mut Criterion) { let exec_scopes = &mut ExecutionScopes::new(); let ids_names = vec!["syscall_ptr"]; @@ -25,9 +25,7 @@ fn criterion_benchmark(c: &mut Criterion) { 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, - ), + cairo_vm::hint_processor::hint_processor_definition::HintReference::new_simple(i), ); } references diff --git a/bench/internals.rs b/bench/internals.rs index 430dc5446..e9b6b1ccf 100644 --- a/bench/internals.rs +++ b/bench/internals.rs @@ -1,5 +1,7 @@ #![deny(warnings)] +mod hashing_running_time; + use cairo_vm::felt; use felt::{felt_str, Felt252}; use lazy_static::lazy_static; diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index 2b46ab11f..aea90d6c1 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -2,7 +2,7 @@ // // Syscall hints // // ************************* -// pub(crate) const DEPLOY: &str = +// pub(crate) const DEPLOY: &'static str = // "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; // pub(crate) const EMIT_EVENT_CODE: &str = @@ -20,14 +20,14 @@ // pub(crate) 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 = -// "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) 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 = -// "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) const LIBRARY_CALL: &str = +// // "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// pub(crate) const CALL_CONTRACT: &str = -// "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) const CALL_CONTRACT: &str = +// // "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; // pub(crate) const GET_TX_SIGNATURE: &str = // "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; @@ -47,19 +47,19 @@ // pub(crate) const GET_BLOCK_NUMBER: &str = // "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// pub(crate) const DELEGATE_CALL: &str = -// "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) const DELEGATE_CALL: &str = +// // "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// pub(crate) const DELEGATE_L1_HANDLER: &str = -// "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) const DELEGATE_L1_HANDLER: &str = +// // "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// pub(crate) const REPLACE_CLASS: &str = -// "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; +// // pub(crate) const REPLACE_CLASS: &str = +// // "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; -// // ************************* -// // Normal hints -// // ************************* +// // // ************************* +// // // Normal hints +// // // ************************* -// pub(crate) 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(crate) 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"; diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 6ffb0b686..065e31652 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -3,10 +3,10 @@ pub mod deprecated_business_logic_syscall_handler; pub mod deprecated_syscall_handler; pub mod deprecated_syscall_request; pub mod deprecated_syscall_response; +pub mod hint_code; pub mod other_syscalls; pub mod syscall_handler; pub mod syscall_handler_errors; pub mod syscall_info; pub mod syscall_request; pub mod syscall_response; -//pub mod hint_code; From 69676ae37c42e774ea51b5bf7f9e9f710bf05a81 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 21 Sep 2023 10:27:41 +0300 Subject: [PATCH 13/24] wip --- Cargo.lock | 148 +++++++++------------------ Cargo.toml | 4 +- bench/hashing_running_time.rs | 4 +- starknet-rs-hint-codegen/Cargo.toml | 25 +++++ starknet-rs-hint-codegen/src/main.rs | 38 +++++++ 5 files changed, 114 insertions(+), 105 deletions(-) create mode 100644 starknet-rs-hint-codegen/Cargo.toml create mode 100644 starknet-rs-hint-codegen/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 44c036f70..d53d742f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstream" version = "0.5.0" @@ -515,17 +521,6 @@ dependencies = [ "syn 2.0.29", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "auto_impl" version = "1.1.0" @@ -1337,24 +1332,40 @@ dependencies = [ ] [[package]] -name = "cipher" -version = "0.4.4" +name = "ciborium" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926" dependencies = [ - "crypto-common", - "inout", + "ciborium-io", + "ciborium-ll", + "serde", ] [[package]] -name = "clap" -version = "2.34.0" +name = "ciborium-io" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656" + +[[package]] +name = "ciborium-ll" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b" dependencies = [ - "bitflags 1.3.2", - "textwrap", - "unicode-width", + "ciborium-io", + "half", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", ] [[package]] @@ -1478,24 +1489,24 @@ dependencies = [ [[package]] name = "criterion" -version = "0.3.6" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" dependencies = [ - "atty", + "anes", "cast", - "clap 2.34.0", + "ciborium", + "clap", "criterion-plot", - "csv", + "is-terminal", "itertools 0.10.5", - "lazy_static", "num-traits 0.2.16", + "once_cell", "oorandom", "plotters", "rayon", "regex", "serde", - "serde_cbor", "serde_derive", "serde_json", "tinytemplate", @@ -1504,9 +1515,9 @@ dependencies = [ [[package]] name = "criterion-plot" -version = "0.4.5" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", "itertools 0.10.5", @@ -1582,27 +1593,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "csv" -version = "1.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086" -dependencies = [ - "csv-core", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "csv-core" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" -dependencies = [ - "memchr", -] - [[package]] name = "ctor" version = "0.2.4" @@ -2171,15 +2161,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.2" @@ -2447,7 +2428,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "hermit-abi 0.3.2", + "hermit-abi", "rustix", "windows-sys", ] @@ -2813,7 +2794,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.3.2", + "hermit-abi", "libc", ] @@ -3600,16 +3581,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde_cbor" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" -dependencies = [ - "half", - "serde", -] - [[package]] name = "serde_derive" version = "1.0.171" @@ -3654,16 +3625,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_json_pythonic" -version = "0.1.2" -source = "git+https://github.com/xJonathanLEI/serde_json_pythonic?tag=v0.1.2#4e4b08bab95309be8672b4efd346bea900040e15" -dependencies = [ - "itoa", - "ryu", - "serde", -] - [[package]] name = "serde_spanned" version = "0.6.3" @@ -3905,7 +3866,7 @@ dependencies = [ "hex", "serde", "serde_json", - "serde_json_pythonic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json_pythonic", "serde_with 2.3.3", "sha3", "starknet-crypto 0.6.0", @@ -4034,7 +3995,7 @@ dependencies = [ "assert_matches", "awc", "cairo-vm", - "clap 4.4.2", + "clap", "coverage-helper", "mimalloc", "num-traits 0.2.16", @@ -4107,7 +4068,7 @@ dependencies = [ "pretty_assertions_sorted", "serde", "serde_json", - "serde_json_pythonic 0.1.2 (git+https://github.com/xJonathanLEI/serde_json_pythonic?tag=v0.1.2)", + "serde_json_pythonic", "sha3", "starknet", "starknet-crypto 0.5.1", @@ -4252,15 +4213,6 @@ dependencies = [ "test-case-core", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.47" @@ -4537,12 +4489,6 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" -[[package]] -name = "unicode-width" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" - [[package]] name = "unicode-xid" version = "0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 112fd0641..b5bf64a93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ once_cell = "1.17.1" starknet = { workspace = true } base64 = { version = "0.21.0", default-features = false, features = ["alloc"] } flate2 = "1.0.25" -serde_json_pythonic = { git = "https://github.com/xJonathanLEI/serde_json_pythonic", tag = "v0.1.2"} +serde_json_pythonic = "0.1.2" phf = "0.11" phf_macros = "0.11" @@ -64,7 +64,7 @@ phf_macros = "0.11" assert_matches = "1.5.0" coverage-helper = "0.1.0" pretty_assertions_sorted = "1.2.3" -criterion = "0.3" +criterion = { version = "0.5.1", features = ["html_reports"] } [[bench]] path = "bench/internals.rs" diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index a32e57418..8c0153a33 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -4,7 +4,6 @@ use cairo_vm::{ types::exec_scope::ExecutionScopes, vm::{runners::cairo_runner::RunResources, vm_core::VirtualMachine}, }; -#[deny(unused_imports)] use criterion::{criterion_group, criterion_main, Criterion}; use starknet_in_rust::syscalls::deprecated_syscall_handler::DeprecatedSyscallHintProcessor; use starknet_in_rust::{ @@ -13,7 +12,8 @@ use starknet_in_rust::{ }; use std::collections::HashMap; use std::{any::Any, hint::black_box}; -#[allow(dead_code)] + + #[allow(dead_code)] fn criterion_benchmark(c: &mut Criterion) { let exec_scopes = &mut ExecutionScopes::new(); let ids_names = vec!["syscall_ptr"]; diff --git a/starknet-rs-hint-codegen/Cargo.toml b/starknet-rs-hint-codegen/Cargo.toml new file mode 100644 index 000000000..465ea052a --- /dev/null +++ b/starknet-rs-hint-codegen/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "starknet-rs-cli" +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"] } +clap = { version = "4.1.8", features = ["derive"] } +actix-web = "4.3.1" +awc = "3.1.1" +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..41f5b5d2e --- /dev/null +++ b/starknet-rs-hint-codegen/src/main.rs @@ -0,0 +1,38 @@ +use std::env; +use std::fs::File; +use std::io::{BufWriter, Write}; +use std::path::Path; + +fn main() { + // let path = Path::new("src").join("syscalls").join("hint_code.rs"); + // let mut file = BufWriter::new(File::create(&path).unwrap()); + + // write!( + // &mut file, + // "static KEYWORDS: phf::Map<&'static str, Keyword> = {}", + // phf_codegen::Map::new() + // .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") + // .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") + // .entry("syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetSequencerAddress") + // .entry("syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageWrite") + // .entry("syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageRead") + // .entry("syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockNumber") + // .entry("syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCall") + // .entry("syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCallL1Handler") + // .entry("syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::CallContract") + // .entry("syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetCallerAddress") + // .entry("syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockTimestamp") + // .entry("syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::SendMessageToL1") + // .entry("syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxSignature") + // .entry("syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxInfo") + // .entry("syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetContractAddress") + // .entry("syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCall") + // .entry("syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCallL1Handler") + // .entry("syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::ReplaceClass") + // .entry( "# 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") + // .entry("ids.is_250 = 1 if ids.addr < 2**250 else 0", "Hint::AddrIs250") + // .build() + // ) + // .unwrap(); + // write!(&mut file, ";\n").unwrap(); +} \ No newline at end of file From 8ff4c3fbcaa4269f76c8d666d2d77b90c72d7a0b Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 21 Sep 2023 10:48:07 +0300 Subject: [PATCH 14/24] wip --- bench/hashing_running_time.rs | 2 +- starknet-rs-hint-codegen/src/main.rs | 60 ++++++++++++++-------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index 8c0153a33..dcfa7d169 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -13,7 +13,7 @@ use starknet_in_rust::{ use std::collections::HashMap; use std::{any::Any, hint::black_box}; - #[allow(dead_code)] +#[allow(dead_code)] fn criterion_benchmark(c: &mut Criterion) { let exec_scopes = &mut ExecutionScopes::new(); let ids_names = vec!["syscall_ptr"]; diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs index 41f5b5d2e..11cb5b2ab 100644 --- a/starknet-rs-hint-codegen/src/main.rs +++ b/starknet-rs-hint-codegen/src/main.rs @@ -4,35 +4,35 @@ use std::io::{BufWriter, Write}; use std::path::Path; fn main() { - // let path = Path::new("src").join("syscalls").join("hint_code.rs"); - // let mut file = BufWriter::new(File::create(&path).unwrap()); + let path = Path::new("src").join("syscalls").join("hint_code.rs"); + let mut file = BufWriter::new(File::create(&path).unwrap()); - // write!( - // &mut file, - // "static KEYWORDS: phf::Map<&'static str, Keyword> = {}", - // phf_codegen::Map::new() - // .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") - // .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") - // .entry("syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetSequencerAddress") - // .entry("syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageWrite") - // .entry("syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageRead") - // .entry("syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockNumber") - // .entry("syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCall") - // .entry("syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCallL1Handler") - // .entry("syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::CallContract") - // .entry("syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetCallerAddress") - // .entry("syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockTimestamp") - // .entry("syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::SendMessageToL1") - // .entry("syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxSignature") - // .entry("syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxInfo") - // .entry("syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetContractAddress") - // .entry("syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCall") - // .entry("syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCallL1Handler") - // .entry("syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::ReplaceClass") - // .entry( "# 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") - // .entry("ids.is_250 = 1 if ids.addr < 2**250 else 0", "Hint::AddrIs250") - // .build() - // ) - // .unwrap(); - // write!(&mut file, ";\n").unwrap(); + write!( + &mut file, + "static KEYWORDS: phf::Map<&'static str, Keyword> = {}", + phf_codegen::Map::new() + .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") + .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") + .entry("syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetSequencerAddress") + .entry("syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageWrite") + .entry("syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageRead") + .entry("syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockNumber") + .entry("syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCall") + .entry("syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCallL1Handler") + .entry("syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::CallContract") + .entry("syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetCallerAddress") + .entry("syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockTimestamp") + .entry("syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::SendMessageToL1") + .entry("syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxSignature") + .entry("syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxInfo") + .entry("syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetContractAddress") + .entry("syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCall") + .entry("syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCallL1Handler") + .entry("syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::ReplaceClass") + .entry( "# 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") + .entry("ids.is_250 = 1 if ids.addr < 2**250 else 0", "Hint::AddrIs250") + .build() + ) + .unwrap(); + write!(&mut file, ";\n").unwrap(); } \ No newline at end of file From a36178516c06dc9f2b8063a39fd96847a8800ac2 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 21 Sep 2023 17:06:56 +0300 Subject: [PATCH 15/24] wip --- Cargo.lock | 38 ++++++++- Cargo.toml | 2 +- src/syscalls/deprecated_syscall_handler.rs | 33 ++------ src/syscalls/hint_code.rs | 98 ++++++++-------------- starknet-rs-hint-codegen/Cargo.toml | 2 +- starknet-rs-hint-codegen/src/main.rs | 2 +- 6 files changed, 81 insertions(+), 94 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b70afb16..3b935383c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1408,6 +1408,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" @@ -1463,6 +1472,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" @@ -3996,9 +4011,28 @@ dependencies = [ "awc", "cairo-vm", "clap", - "coverage-helper", + "coverage-helper 0.2.0", + "mimalloc", + "num-traits 0.2.16", + "serde", + "starknet_in_rust", +] + +[[package]] +name = "starknet-rs-hint-codegen" +version = "0.4.0" +dependencies = [ + "actix-web", + "assert_matches", + "awc", + "cairo-vm", + "clap", + "codegen", + "coverage-helper 0.1.1", "mimalloc", "num-traits 0.2.16", + "phf", + "phf_codegen", "serde", "starknet_in_rust", ] @@ -4050,7 +4084,7 @@ dependencies = [ "cairo-lang-starknet", "cairo-lang-utils", "cairo-vm", - "coverage-helper", + "coverage-helper 0.2.0", "criterion", "flate2", "getset", diff --git a/Cargo.toml b/Cargo.toml index 3c19e1542..c3b122e16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ cairo_1_tests = [] metrics = [] [workspace] -members = ["cli", "fuzzer", "rpc_state_reader"] +members = ["cli", "fuzzer", "rpc_state_reader", "starknet-rs-hint-codegen"] [workspace.dependencies] cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] } diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 649c06690..1982c9700 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -20,10 +20,10 @@ use cairo_vm::{ types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; -use phf_macros::phf_map; use std::{any::Any, collections::HashMap}; -pub enum Hint { +#[derive(Clone)] +pub(crate) enum Hint { Deploy, EmitEvent, GetBlockNumber, @@ -47,28 +47,13 @@ pub enum Hint { AddrIs250, } -static HINT_CODES: phf::Map<&'static str, Hint> = phf_map! { - "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::Deploy, - "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::EmitEvent, - "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetSequencerAddress, - "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::StorageWrite, - "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::StorageRead, - "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetBlockNumber, - "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::LibraryCall, - "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::LibraryCallL1Handler, - "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::CallContract, - "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetCallerAddress, - "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetBlockTimestamp, - "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::SendMessageToL1, - "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetTxSignature, - "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetTxInfo, - "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::GetContractAddress, - "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::DelegateCall, - "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::DelegateCallL1Handler, - "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)" => Hint::ReplaceClass, - "# 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, - "ids.is_250 = 1 if ids.addr < 2**250 else 0" => Hint::AddrIs250, -}; +include!("hint_code.rs"); + +pub fn parse_keyword(keyword: &str) -> Option { + KEYWORDS.get(keyword).cloned() +} + + /// Definition of the deprecated syscall hint processor with associated structs pub struct DeprecatedSyscallHintProcessor<'a, S: StateReader> { diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index aea90d6c1..1d6e672dc 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -1,65 +1,33 @@ -// // ************************* -// // Syscall hints -// // ************************* - -// pub(crate) const DEPLOY: &'static str = -// "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const EMIT_EVENT_CODE: &str = -// "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_SEQUENCER_ADDRESS: &str = -// "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const STORAGE_WRITE: &str = -// "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const STORAGE_READ: &str = -// "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) 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 = -// // "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // pub(crate) const LIBRARY_CALL: &str = -// // "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // pub(crate) const CALL_CONTRACT: &str = -// // "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_TX_SIGNATURE: &str = -// "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_TX_INFO: &str = -// "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_CONTRACT_ADDRESS: &str = -// "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_CALLER_ADDRESS: &str = -// "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_BLOCK_TIMESTAMP: &str = -// "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// pub(crate) const GET_BLOCK_NUMBER: &str = -// "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // pub(crate) const DELEGATE_CALL: &str = -// // "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // pub(crate) const DELEGATE_L1_HANDLER: &str = -// // "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // pub(crate) const REPLACE_CLASS: &str = -// // "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; - -// // // ************************* -// // // Normal hints -// // // ************************* - -// // // pub(crate) 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"; +use super::deprecated_syscall_handler::Hint; + +static KEYWORDS: 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)", super::deprecated_syscall_handler::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 index 465ea052a..312d82a57 100644 --- a/starknet-rs-hint-codegen/Cargo.toml +++ b/starknet-rs-hint-codegen/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "starknet-rs-cli" +name = "starknet-rs-hint-codegen" version = "0.4.0" edition = "2021" diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs index 11cb5b2ab..6178b3836 100644 --- a/starknet-rs-hint-codegen/src/main.rs +++ b/starknet-rs-hint-codegen/src/main.rs @@ -9,7 +9,7 @@ fn main() { write!( &mut file, - "static KEYWORDS: phf::Map<&'static str, Keyword> = {}", + "use super::deprecated_syscall_handler::Hint;\n\nstatic KEYWORDS: phf::Map<&'static str, Hint> = {}", phf_codegen::Map::new() .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") From 2c0427de4e04390dac3f4b055d2d4bbddab8ed53 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Fri, 22 Sep 2023 09:30:08 +0300 Subject: [PATCH 16/24] fix format --- src/syscalls/deprecated_syscall_handler.rs | 12 ++---------- src/syscalls/hint_code.rs | 2 +- starknet-rs-hint-codegen/src/main.rs | 9 ++++----- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 1982c9700..8008f166a 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -23,7 +23,7 @@ use cairo_vm::{ use std::{any::Any, collections::HashMap}; #[derive(Clone)] -pub(crate) enum Hint { +pub enum Hint { Deploy, EmitEvent, GetBlockNumber, @@ -47,14 +47,6 @@ pub(crate) enum Hint { AddrIs250, } -include!("hint_code.rs"); - -pub fn parse_keyword(keyword: &str) -> Option { - KEYWORDS.get(keyword).cloned() -} - - - /// Definition of the deprecated syscall hint processor with associated structs pub struct DeprecatedSyscallHintProcessor<'a, S: StateReader> { pub(crate) builtin_hint_processor: BuiltinHintProcessor, @@ -110,7 +102,7 @@ impl<'a, S: StateReader> DeprecatedSyscallHintProcessor<'a, S> { let hint_code = &hint_data.code; let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - if let Some(syscall) = HINT_CODES.get(hint_code) { + if let Some(syscall) = super::hint_code::HINTCODE.get(hint_code) { match syscall { Hint::AddrBoundPrime => { other_syscalls::addr_bound_prime(vm, hint_data, constants)?; diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index 1d6e672dc..dc3b673f0 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -1,6 +1,6 @@ use super::deprecated_syscall_handler::Hint; -static KEYWORDS: phf::Map<&'static str, Hint> = ::phf::Map { +pub(crate) static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { key: 12913932095322966823, disps: &[ (1, 0), diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs index 6178b3836..9ec9f202f 100644 --- a/starknet-rs-hint-codegen/src/main.rs +++ b/starknet-rs-hint-codegen/src/main.rs @@ -1,15 +1,14 @@ -use std::env; use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; fn main() { let path = Path::new("src").join("syscalls").join("hint_code.rs"); - let mut file = BufWriter::new(File::create(&path).unwrap()); + let mut file = BufWriter::new(File::create(path).unwrap()); write!( &mut file, - "use super::deprecated_syscall_handler::Hint;\n\nstatic KEYWORDS: phf::Map<&'static str, Hint> = {}", + "use super::deprecated_syscall_handler::Hint;\n\nstatic HINTCODE: phf::Map<&'static str, Hint> = {}", phf_codegen::Map::new() .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") @@ -34,5 +33,5 @@ fn main() { .build() ) .unwrap(); - write!(&mut file, ";\n").unwrap(); -} \ No newline at end of file + writeln!(&mut file, ";").unwrap(); +} From 8d7b23acad2501356569b59d09f9fbbef924f4f3 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 28 Sep 2023 13:33:51 +0300 Subject: [PATCH 17/24] use hint_code in hint_code_map --- src/syscalls/deprecated_syscall_handler.rs | 2 +- src/syscalls/hint_code.rs | 98 ++++++++++++++-------- src/syscalls/hint_code_map.rs | 33 ++++++++ src/syscalls/mod.rs | 1 + starknet-rs-hint-codegen/src/main.rs | 47 ++++++----- 5 files changed, 125 insertions(+), 56 deletions(-) create mode 100644 src/syscalls/hint_code_map.rs diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 8008f166a..811aed695 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -102,7 +102,7 @@ impl<'a, S: StateReader> DeprecatedSyscallHintProcessor<'a, S> { let hint_code = &hint_data.code; let syscall_ptr = get_syscall_ptr(vm, &hint_data.ids_data, &hint_data.ap_tracking)?; - if let Some(syscall) = super::hint_code::HINTCODE.get(hint_code) { + if let Some(syscall) = super::hint_code_map::HINTCODE.get(hint_code) { match syscall { Hint::AddrBoundPrime => { other_syscalls::addr_bound_prime(vm, hint_data, constants)?; diff --git a/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index dc3b673f0..21591afff 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -1,33 +1,65 @@ -use super::deprecated_syscall_handler::Hint; - -pub(crate) 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)", super::deprecated_syscall_handler::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), - ], -}; +// ************************* +// Syscall hints +// ************************* + +pub const DEPLOY: &str = + "syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const EMIT_EVENT_CODE: &str = + "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_SEQUENCER_ADDRESS: &str = + "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const STORAGE_WRITE: &str = + "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const STORAGE_READ: &str = + "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const SEND_MESSAGE_TO_L1: &str = + "syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const LIBRARY_CALL_L1_HANDLER: &str = + "syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const LIBRARY_CALL: &str = + "syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const CALL_CONTRACT: &str = + "syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_TX_SIGNATURE: &str = + "syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_TX_INFO: &str = + "syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_CONTRACT_ADDRESS: &str = + "syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_CALLER_ADDRESS: &str = + "syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_BLOCK_TIMESTAMP: &str = + "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const GET_BLOCK_NUMBER: &str = + "syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const DELEGATE_CALL: &str = + "syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const DELEGATE_L1_HANDLER: &str = + "syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +pub const REPLACE_CLASS: &str = + "syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)"; + +// ************************* +// Normal hints +// ************************* + +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 const ADDR_IS_250: &str = "ids.is_250 = 1 if ids.addr < 2**250 else 0"; \ No newline at end of file diff --git a/src/syscalls/hint_code_map.rs b/src/syscalls/hint_code_map.rs new file mode 100644 index 000000000..73d63dfb3 --- /dev/null +++ b/src/syscalls/hint_code_map.rs @@ -0,0 +1,33 @@ +use super::{deprecated_syscall_handler::Hint, hint_code}; + +pub(crate) static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { + key: 7485420634051515786, + disps: &[ + (1, 19), + (2, 0), + (0, 8), + (7, 4), + ], + entries: &[ + (hint_code::GET_BLOCK_TIMESTAMP, Hint::GetBlockTimestamp), + (hint_code::GET_BLOCK_NUMBER, Hint::GetBlockNumber), + (hint_code::GET_TX_SIGNATURE, Hint::GetTxSignature), + (hint_code::STORAGE_WRITE, Hint::StorageWrite), + (hint_code::CALL_CONTRACT, Hint::CallContract), + (hint_code::LIBRARY_CALL, Hint::LibraryCall), + (hint_code::STORAGE_READ, Hint::StorageRead), + (hint_code::SEND_MESSAGE_TO_L1, Hint::SendMessageToL1), + (hint_code::REPLACE_CLASS, Hint::ReplaceClass), + (hint_code::GET_TX_INFO, Hint::GetTxInfo), + (hint_code::GET_CONTRACT_ADDRESS, Hint::GetContractAddress), + (hint_code::GET_SEQUENCER_ADDRESS, Hint::GetSequencerAddress), + (hint_code::ADDR_BOUND_PRIME, Hint::AddrBoundPrime), + (hint_code::LIBRARY_CALL_L1_HANDLER, Hint::LibraryCallL1Handler), + (hint_code::DELEGATE_CALL, Hint::DelegateCall), + (hint_code::DELEGATE_L1_HANDLER, Hint::DelegateCallL1Handler), + (hint_code::EMIT_EVENT_CODE, Hint::EmitEvent), + (hint_code::DEPLOY, Hint::Deploy), + (hint_code::GET_CALLER_ADDRESS, Hint::GetCallerAddress), + (hint_code::ADDR_IS_250, Hint::AddrIs250), + ], +}; diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 065e31652..e59fcf9d5 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -10,3 +10,4 @@ pub mod syscall_handler_errors; pub mod syscall_info; pub mod syscall_request; pub mod syscall_response; +pub mod hint_code_map; diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs index 9ec9f202f..ea058cc3b 100644 --- a/starknet-rs-hint-codegen/src/main.rs +++ b/starknet-rs-hint-codegen/src/main.rs @@ -1,35 +1,38 @@ use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; +use starknet_in_rust::syscalls::hint_code; +use starknet_in_rust::syscalls::hint_code::*; + fn main() { - let path = Path::new("src").join("syscalls").join("hint_code.rs"); + 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\nstatic HINTCODE: phf::Map<&'static str, Hint> = {}", + "use super::deprecated_syscall_handler::Hint;\n\npub(crate) static HINTCODE: phf::Map<&'static str, Hint> = {}", phf_codegen::Map::new() - .entry("syscall_handler.deploy(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::Deploy") - .entry("syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::EmitEvent") - .entry("syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetSequencerAddress") - .entry("syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageWrite") - .entry("syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::StorageRead") - .entry("syscall_handler.get_block_number(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockNumber") - .entry("syscall_handler.library_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCall") - .entry("syscall_handler.library_call_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::LibraryCallL1Handler") - .entry("syscall_handler.call_contract(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::CallContract") - .entry("syscall_handler.get_caller_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetCallerAddress") - .entry("syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetBlockTimestamp") - .entry("syscall_handler.send_message_to_l1(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::SendMessageToL1") - .entry("syscall_handler.get_tx_signature(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxSignature") - .entry("syscall_handler.get_tx_info(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetTxInfo") - .entry("syscall_handler.get_contract_address(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::GetContractAddress") - .entry("syscall_handler.delegate_call(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCall") - .entry("syscall_handler.delegate_l1_handler(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::DelegateCallL1Handler") - .entry("syscall_handler.replace_class(segments=segments, syscall_ptr=ids.syscall_ptr)", "Hint::ReplaceClass") - .entry( "# 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") - .entry("ids.is_250 = 1 if ids.addr < 2**250 else 0", "Hint::AddrIs250") + .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(); From b611e4ed9f95b188eea6e8c73e4abfc507c93e22 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 28 Sep 2023 13:34:49 +0300 Subject: [PATCH 18/24] use hint_code in hint_code_map --- Cargo.lock | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b935383c..d269b9022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1538,16 +1538,6 @@ dependencies = [ "itertools 0.10.5", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -2803,16 +2793,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "object" version = "0.32.0" @@ -3208,9 +3188,9 @@ checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" [[package]] name = "rayon" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" dependencies = [ "either", "rayon-core", @@ -3218,14 +3198,12 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" dependencies = [ - "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "num_cpus", ] [[package]] @@ -4726,9 +4704,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] From 12b29ef291e76a0d475e7e04fd6c26656909f7d3 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 28 Sep 2023 16:02:52 +0300 Subject: [PATCH 19/24] Added Makefile line to regerate hint_map if necessary --- Makefile | 3 ++ src/syscalls/hint_code.rs | 18 +++++----- src/syscalls/hint_code_map.rs | 52 ++++++++++++++-------------- src/syscalls/mod.rs | 2 +- starknet-rs-hint-codegen/src/main.rs | 3 +- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 2d90d94cf..ae1456e70 100644 --- a/Makefile +++ b/Makefile @@ -209,3 +209,6 @@ benchmark: compile-cairo compile-starknet ./scripts/bench-deploy-invoke.sh ./scripts/bench-fibonacci.sh ./scripts/bench-deploy.sh + +regenerate-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/src/syscalls/hint_code.rs b/src/syscalls/hint_code.rs index 21591afff..48851e32f 100644 --- a/src/syscalls/hint_code.rs +++ b/src/syscalls/hint_code.rs @@ -2,25 +2,24 @@ // Syscall hints // ************************* -pub 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 const EMIT_EVENT_CODE: &str = +pub const EMIT_EVENT_CODE: &str = "syscall_handler.emit_event(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub const GET_SEQUENCER_ADDRESS: &str = +pub const GET_SEQUENCER_ADDRESS: &str = "syscall_handler.get_sequencer_address(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub const STORAGE_WRITE: &str = +pub const STORAGE_WRITE: &str = "syscall_handler.storage_write(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub const STORAGE_READ: &str = +pub const STORAGE_READ: &str = "syscall_handler.storage_read(segments=segments, syscall_ptr=ids.syscall_ptr)"; -pub 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 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 const LIBRARY_CALL: &str = @@ -62,4 +61,5 @@ pub const REPLACE_CLASS: &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 const ADDR_IS_250: &str = "ids.is_250 = 1 if ids.addr < 2**250 else 0"; \ No newline at end of file + +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 index 73d63dfb3..5e066b132 100644 --- a/src/syscalls/hint_code_map.rs +++ b/src/syscalls/hint_code_map.rs @@ -1,33 +1,33 @@ -use super::{deprecated_syscall_handler::Hint, hint_code}; +use super::deprecated_syscall_handler::Hint; pub(crate) static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { - key: 7485420634051515786, + key: 12913932095322966823, disps: &[ - (1, 19), - (2, 0), - (0, 8), - (7, 4), + (1, 0), + (3, 14), + (8, 4), + (7, 18), ], entries: &[ - (hint_code::GET_BLOCK_TIMESTAMP, Hint::GetBlockTimestamp), - (hint_code::GET_BLOCK_NUMBER, Hint::GetBlockNumber), - (hint_code::GET_TX_SIGNATURE, Hint::GetTxSignature), - (hint_code::STORAGE_WRITE, Hint::StorageWrite), - (hint_code::CALL_CONTRACT, Hint::CallContract), - (hint_code::LIBRARY_CALL, Hint::LibraryCall), - (hint_code::STORAGE_READ, Hint::StorageRead), - (hint_code::SEND_MESSAGE_TO_L1, Hint::SendMessageToL1), - (hint_code::REPLACE_CLASS, Hint::ReplaceClass), - (hint_code::GET_TX_INFO, Hint::GetTxInfo), - (hint_code::GET_CONTRACT_ADDRESS, Hint::GetContractAddress), - (hint_code::GET_SEQUENCER_ADDRESS, Hint::GetSequencerAddress), - (hint_code::ADDR_BOUND_PRIME, Hint::AddrBoundPrime), - (hint_code::LIBRARY_CALL_L1_HANDLER, Hint::LibraryCallL1Handler), - (hint_code::DELEGATE_CALL, Hint::DelegateCall), - (hint_code::DELEGATE_L1_HANDLER, Hint::DelegateCallL1Handler), - (hint_code::EMIT_EVENT_CODE, Hint::EmitEvent), - (hint_code::DEPLOY, Hint::Deploy), - (hint_code::GET_CALLER_ADDRESS, Hint::GetCallerAddress), - (hint_code::ADDR_IS_250, Hint::AddrIs250), + ("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/src/syscalls/mod.rs b/src/syscalls/mod.rs index e59fcf9d5..97a44cecb 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -4,10 +4,10 @@ pub mod deprecated_syscall_handler; pub mod deprecated_syscall_request; pub mod deprecated_syscall_response; pub mod hint_code; +pub mod hint_code_map; pub mod other_syscalls; pub mod syscall_handler; pub mod syscall_handler_errors; pub mod syscall_info; pub mod syscall_request; pub mod syscall_response; -pub mod hint_code_map; diff --git a/starknet-rs-hint-codegen/src/main.rs b/starknet-rs-hint-codegen/src/main.rs index ea058cc3b..07416529c 100644 --- a/starknet-rs-hint-codegen/src/main.rs +++ b/starknet-rs-hint-codegen/src/main.rs @@ -1,9 +1,8 @@ use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; -use starknet_in_rust::syscalls::hint_code; -use starknet_in_rust::syscalls::hint_code::*; +use starknet_in_rust::syscalls::hint_code; fn main() { let path = Path::new("src").join("syscalls").join("hint_code_map.rs"); From b5a6d4a527b7a77a5295893135b4642abef50783 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 28 Sep 2023 18:07:08 +0300 Subject: [PATCH 20/24] Modifile Makefile line to be regenerate map-hints --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ae1456e70..22cc1035e 100644 --- a/Makefile +++ b/Makefile @@ -210,5 +210,5 @@ benchmark: compile-cairo compile-starknet ./scripts/bench-fibonacci.sh ./scripts/bench-deploy.sh -regenerate-hints:: +regenerate map-hints: cargo run --bin starknet-rs-hint-codegen --manifest-path starknet-rs-hint-codegen/Cargo.toml \ No newline at end of file From 0b1a9faab3ca1d324dfcaabbcd8d6ba6c46f6f4b Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Fri, 29 Sep 2023 15:55:21 +0300 Subject: [PATCH 21/24] test without the #[allow(dead_code)] --- bench/hashing_running_time.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index dcfa7d169..426273b17 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -13,7 +13,6 @@ use starknet_in_rust::{ use std::collections::HashMap; use std::{any::Any, hint::black_box}; -#[allow(dead_code)] fn criterion_benchmark(c: &mut Criterion) { let exec_scopes = &mut ExecutionScopes::new(); let ids_names = vec!["syscall_ptr"]; From 7f08a2c092f8ae1d23b9589d540925231eb43fb1 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Fri, 29 Sep 2023 17:13:58 +0300 Subject: [PATCH 22/24] resolve dead code --- bench/internals.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/bench/internals.rs b/bench/internals.rs index e9b6b1ccf..4d4d837d1 100644 --- a/bench/internals.rs +++ b/bench/internals.rs @@ -1,7 +1,4 @@ #![deny(warnings)] - -mod hashing_running_time; - use cairo_vm::felt; use felt::{felt_str, Felt252}; use lazy_static::lazy_static; From e54d5e8b9605255b27e8ee1fac3fa4d6b7de1138 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Mon, 2 Oct 2023 14:39:38 +0300 Subject: [PATCH 23/24] wip --- bench/hashing_running_time.rs | 61 ++++++++++++---------- src/syscalls/deprecated_syscall_handler.rs | 2 +- src/syscalls/hint_code_map.rs | 2 +- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/bench/hashing_running_time.rs b/bench/hashing_running_time.rs index 426273b17..fb0ad6ab4 100644 --- a/bench/hashing_running_time.rs +++ b/bench/hashing_running_time.rs @@ -6,6 +6,7 @@ use cairo_vm::{ }; 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, @@ -30,37 +31,39 @@ fn criterion_benchmark(c: &mut Criterion) { references }; - let mut ids_data = - HashMap::::new( - ); - for (i, name) in ids_names.iter().enumerate() { - ids_data.insert(name.to_string(), references.get(&i).unwrap().clone()); - } + 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( - "syscall_handler.get_block_timestamp(segments=segments, syscall_ptr=ids.syscall_ptr)" - .to_string(), - ids_data, - )); + let hint_data: Box = Box::new(HintProcessorData::new_default( + hint_code.to_string(), + ids_data.clone(), + )); - // Benchmark the execute_hint method - c.bench_function("execute_hint", |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, - )); - }) - }); + // 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); diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index 811aed695..ebe8801af 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -22,7 +22,7 @@ use cairo_vm::{ }; use std::{any::Any, collections::HashMap}; -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum Hint { Deploy, EmitEvent, diff --git a/src/syscalls/hint_code_map.rs b/src/syscalls/hint_code_map.rs index 5e066b132..e59f1b0eb 100644 --- a/src/syscalls/hint_code_map.rs +++ b/src/syscalls/hint_code_map.rs @@ -1,6 +1,6 @@ use super::deprecated_syscall_handler::Hint; -pub(crate) static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { +pub static HINTCODE: phf::Map<&'static str, Hint> = ::phf::Map { key: 12913932095322966823, disps: &[ (1, 0), From 66e1a82a4a55c5bb3a503aef713e92c08b8d2916 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Tue, 3 Oct 2023 16:51:10 +0300 Subject: [PATCH 24/24] remove unsued dependencies awc , actix-web and clap --- Cargo.lock | 3 --- starknet-rs-hint-codegen/Cargo.toml | 3 --- 2 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9eec8efa..9bdeaba5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4067,11 +4067,8 @@ dependencies = [ name = "starknet-rs-hint-codegen" version = "0.4.0" dependencies = [ - "actix-web", "assert_matches", - "awc", "cairo-vm", - "clap", "codegen", "coverage-helper 0.1.1", "mimalloc", diff --git a/starknet-rs-hint-codegen/Cargo.toml b/starknet-rs-hint-codegen/Cargo.toml index 312d82a57..967d6e856 100644 --- a/starknet-rs-hint-codegen/Cargo.toml +++ b/starknet-rs-hint-codegen/Cargo.toml @@ -12,9 +12,6 @@ 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"] } -clap = { version = "4.1.8", features = ["derive"] } -actix-web = "4.3.1" -awc = "3.1.1" mimalloc = { version = "0.1.29", default-features = false, optional = true } codegen = "0.2.0" phf = { version = "0.11.1", default-features = false }