From f00371f9f7cea66b40f30a586389e1a914a28fff Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 23 May 2026 13:29:15 +0300 Subject: [PATCH] starknet_os: os resources test - add emit event --- ...blockifier_versioned_constants_0_14_4.json | 2 +- .../0.14.3_0.14.4.txt | 1 + .../src/transaction/execution_flavors_test.rs | 3 +- .../cairo1/os_resources_test_contract.cairo | 7 ++- .../src/os_resources_test.rs | 51 +++++++++++++++---- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json b/crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json index 4a7fa619c08..34904ce542f 100644 --- a/crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json +++ b/crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json @@ -264,7 +264,7 @@ } }, "EmitEvent": { - "n_steps": 61, + "n_steps": 47, "n_memory_holes": 0, "builtin_instance_counter": { "range_check_builtin": 1 diff --git a/crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt b/crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt index 3bcf0fd28f8..b4cbf4891cc 100644 --- a/crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt +++ b/crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt @@ -1,4 +1,5 @@ + /os_constants/allowed_virtual_os_program_hashes/2: "0x28619dbc9767792fb536aaba7a2d55f70faadf808c95ec66b956d33fdee1bc0" ~ /os_resources/execute_syscalls/CallContract/n_steps: 901 +~ /os_resources/execute_syscalls/EmitEvent/n_steps: 47 ~ /os_resources/execute_syscalls/LibraryCall/n_steps: 874 ~ /os_resources/execute_syscalls/MetaTxV0/constant/n_steps: 1307 diff --git a/crates/blockifier/src/transaction/execution_flavors_test.rs b/crates/blockifier/src/transaction/execution_flavors_test.rs index b248fb90348..a06fcefb218 100644 --- a/crates/blockifier/src/transaction/execution_flavors_test.rs +++ b/crates/blockifier/src/transaction/execution_flavors_test.rs @@ -906,7 +906,8 @@ fn test_simulate_validate_charge_fee_post_execution( + 4 * u64_from_usize( get_const_syscall_resources(SyscallSelector::StorageWrite).n_steps, ) - + 3588) + + u64_from_usize(get_const_syscall_resources(SyscallSelector::EmitEvent).n_steps) + + 3527) .into(), validate, &fee_type, diff --git a/crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo b/crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo index e8e69e0e55e..49682b3ba1e 100644 --- a/crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo +++ b/crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo @@ -2,7 +2,9 @@ #[starknet::contract(account)] mod OsResourcesTestContract { use starknet::info::SyscallResultTrait; - use starknet::syscalls::{call_contract_syscall, deploy_syscall, library_call_syscall}; + use starknet::syscalls::{ + call_contract_syscall, deploy_syscall, emit_event_syscall, library_call_syscall, + }; use starknet::{ClassHash, ContractAddress}; const STABLE_EXTERNAL_ENTRY_POINT_SELECTOR: felt252 = selector!("external"); @@ -73,5 +75,8 @@ mod OsResourcesTestContract { deploy_syscall(stable_class_hash, 1, array![0].span(), true).unwrap_syscall(); // linear factor (calldata len = 1): deploy_syscall(stable_class_hash, 1, array![1, 0].span(), true).unwrap_syscall(); + + // emit event syscall. + emit_event_syscall(array![5].span(), array![7].span()).unwrap_syscall(); } } diff --git a/crates/starknet_os_flow_tests/src/os_resources_test.rs b/crates/starknet_os_flow_tests/src/os_resources_test.rs index e392d3a1cf4..0dc6143d498 100644 --- a/crates/starknet_os_flow_tests/src/os_resources_test.rs +++ b/crates/starknet_os_flow_tests/src/os_resources_test.rs @@ -35,14 +35,18 @@ use crate::special_contracts::{ DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_CASM, DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_SIERRA, }; -use crate::test_manager::{TestBuilder, TestBuilderConfig, FUNDED_ACCOUNT_ADDRESS}; +use crate::test_manager::{ + EventPredicateExpectation, + TestBuilder, + TestBuilderConfig, + FUNDED_ACCOUNT_ADDRESS, +}; use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS; // TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls. -const UNMEASURABLE_SYSCALLS: [Selector; 32] = [ +const UNMEASURABLE_SYSCALLS: [Selector; 31] = [ Selector::DelegateCall, Selector::DelegateL1Handler, - Selector::EmitEvent, Selector::GetBlockHash, Selector::GetBlockNumber, Selector::GetBlockTimestamp, @@ -191,7 +195,19 @@ async fn test_os_resources_regression() { &test_builder.chain_id(), ) .unwrap(); - test_builder.add_invoke_tx(tx, None, None); + test_builder.add_invoke_tx( + tx, + None, + // Expect one event from the emit-event syscall measurement. + Some(vec![EventPredicateExpectation { + description: "emit event syscall".to_string(), + predicate: Box::new(move |event| { + event.from_address == os_resources_contract_address + && event.content.keys[0].0 == Felt::from(5) + && event.content.data.0[0] == Felt::from(7) + }), + }]), + ); // Run test. Grab the execution info from the runner (for later) before consuming it. let test_runner = test_builder.build().await; @@ -212,9 +228,25 @@ async fn test_os_resources_regression() { let test_output = test_runner.run(); test_output.perform_default_validations(); - // Extract syscall resources consumed per syscall. let mut inner_calls_iter = inner_calls.into_iter(); - let syscall_traces = test_output.runner_output.txs_trace.last().unwrap().get_syscalls(); + + // Extract syscall resources consumed per syscall. + // There should be two events emitted: the first is the syscall we are measuring, and the second + // is the last syscall in the tx, emitted from the fee transfer. Pop the second event. + let mut syscall_traces = + test_output.runner_output.txs_trace.last().unwrap().get_syscalls().clone(); + assert!(!UNMEASURABLE_SYSCALLS.contains(&Selector::EmitEvent)); + assert_eq!( + syscall_traces + .iter() + .filter(|syscall_trace| syscall_trace.get_selector() == Selector::EmitEvent) + .count(), + 2 + ); + assert_eq!(syscall_traces.pop().unwrap().get_selector(), Selector::EmitEvent); + + // Measure each syscall overhead. If the syscall incurs an inner call, subtract the inner call + // overhead. let mut syscalls_iter = syscall_traces .iter() .filter(|syscall_trace| !UNMEASURABLE_SYSCALLS.contains(&syscall_trace.get_selector())); @@ -223,7 +255,7 @@ async fn test_os_resources_regression() { let mut maybe_deduct_inner = |total: ExecutionResources, selector: Selector| -> ExecutionResources { // We assume no inner calls have nested inner calls (all inner calls are leaves). - if selector.is_calling_syscall() { + (if selector.is_calling_syscall() { // TODO(Dori): Take opcodes (like blake) into account, instead of using the // vm_resources field. // TODO(Dori): Consider supporting memory-hole counting in the OsLogger. Until then, @@ -231,10 +263,11 @@ async fn test_os_resources_regression() { // OsLogger resources. let mut to_deduct = inner_calls_iter.next().unwrap().resources.vm_resources; to_deduct.n_memory_holes = 0; - (&total - &to_deduct).filter_unused_builtins() + &total - &to_deduct } else { total - } + }) + .filter_unused_builtins() }; while let Some(syscall_trace) = syscalls_iter.next() { let selector = syscall_trace.get_selector();