Skip to content

Commit fd9c8c3

Browse files
starknet_os: os resources test - add emit event (#14136)
1 parent 0561270 commit fd9c8c3

5 files changed

Lines changed: 52 additions & 12 deletions

File tree

crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
}
265265
},
266266
"EmitEvent": {
267-
"n_steps": 61,
267+
"n_steps": 47,
268268
"n_memory_holes": 0,
269269
"builtin_instance_counter": {
270270
"range_check_builtin": 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x28619dbc9767792fb536aaba7a2d55f70faadf808c95ec66b956d33fdee1bc0"
22
~ /os_resources/execute_syscalls/CallContract/n_steps: 901
3+
~ /os_resources/execute_syscalls/EmitEvent/n_steps: 47
34
~ /os_resources/execute_syscalls/LibraryCall/n_steps: 874
45
~ /os_resources/execute_syscalls/MetaTxV0/constant/n_steps: 1307

crates/blockifier/src/transaction/execution_flavors_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,8 @@ fn test_simulate_validate_charge_fee_post_execution(
906906
+ 4 * u64_from_usize(
907907
get_const_syscall_resources(SyscallSelector::StorageWrite).n_steps,
908908
)
909-
+ 3588)
909+
+ u64_from_usize(get_const_syscall_resources(SyscallSelector::EmitEvent).n_steps)
910+
+ 3527)
910911
.into(),
911912
validate,
912913
&fee_type,

crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#[starknet::contract(account)]
33
mod OsResourcesTestContract {
44
use starknet::info::SyscallResultTrait;
5-
use starknet::syscalls::{call_contract_syscall, deploy_syscall, library_call_syscall};
5+
use starknet::syscalls::{
6+
call_contract_syscall, deploy_syscall, emit_event_syscall, library_call_syscall,
7+
};
68
use starknet::{ClassHash, ContractAddress};
79

810
const STABLE_EXTERNAL_ENTRY_POINT_SELECTOR: felt252 = selector!("external");
@@ -73,5 +75,8 @@ mod OsResourcesTestContract {
7375
deploy_syscall(stable_class_hash, 1, array![0].span(), true).unwrap_syscall();
7476
// linear factor (calldata len = 1):
7577
deploy_syscall(stable_class_hash, 1, array![1, 0].span(), true).unwrap_syscall();
78+
79+
// emit event syscall.
80+
emit_event_syscall(array![5].span(), array![7].span()).unwrap_syscall();
7681
}
7782
}

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ use crate::special_contracts::{
3535
DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_CASM,
3636
DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_SIERRA,
3737
};
38-
use crate::test_manager::{TestBuilder, TestBuilderConfig, FUNDED_ACCOUNT_ADDRESS};
38+
use crate::test_manager::{
39+
EventPredicateExpectation,
40+
TestBuilder,
41+
TestBuilderConfig,
42+
FUNDED_ACCOUNT_ADDRESS,
43+
};
3944
use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS;
4045

4146
// TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls.
42-
const UNMEASURABLE_SYSCALLS: [Selector; 32] = [
47+
const UNMEASURABLE_SYSCALLS: [Selector; 31] = [
4348
Selector::DelegateCall,
4449
Selector::DelegateL1Handler,
45-
Selector::EmitEvent,
4650
Selector::GetBlockHash,
4751
Selector::GetBlockNumber,
4852
Selector::GetBlockTimestamp,
@@ -191,7 +195,19 @@ async fn test_os_resources_regression() {
191195
&test_builder.chain_id(),
192196
)
193197
.unwrap();
194-
test_builder.add_invoke_tx(tx, None, None);
198+
test_builder.add_invoke_tx(
199+
tx,
200+
None,
201+
// Expect one event from the emit-event syscall measurement.
202+
Some(vec![EventPredicateExpectation {
203+
description: "emit event syscall".to_string(),
204+
predicate: Box::new(move |event| {
205+
event.from_address == os_resources_contract_address
206+
&& event.content.keys[0].0 == Felt::from(5)
207+
&& event.content.data.0[0] == Felt::from(7)
208+
}),
209+
}]),
210+
);
195211

196212
// Run test. Grab the execution info from the runner (for later) before consuming it.
197213
let test_runner = test_builder.build().await;
@@ -212,9 +228,25 @@ async fn test_os_resources_regression() {
212228
let test_output = test_runner.run();
213229
test_output.perform_default_validations();
214230

215-
// Extract syscall resources consumed per syscall.
216231
let mut inner_calls_iter = inner_calls.into_iter();
217-
let syscall_traces = test_output.runner_output.txs_trace.last().unwrap().get_syscalls();
232+
233+
// Extract syscall resources consumed per syscall.
234+
// There should be two events emitted: the first is the syscall we are measuring, and the second
235+
// is the last syscall in the tx, emitted from the fee transfer. Pop the second event.
236+
let mut syscall_traces =
237+
test_output.runner_output.txs_trace.last().unwrap().get_syscalls().clone();
238+
assert!(!UNMEASURABLE_SYSCALLS.contains(&Selector::EmitEvent));
239+
assert_eq!(
240+
syscall_traces
241+
.iter()
242+
.filter(|syscall_trace| syscall_trace.get_selector() == Selector::EmitEvent)
243+
.count(),
244+
2
245+
);
246+
assert_eq!(syscall_traces.pop().unwrap().get_selector(), Selector::EmitEvent);
247+
248+
// Measure each syscall overhead. If the syscall incurs an inner call, subtract the inner call
249+
// overhead.
218250
let mut syscalls_iter = syscall_traces
219251
.iter()
220252
.filter(|syscall_trace| !UNMEASURABLE_SYSCALLS.contains(&syscall_trace.get_selector()));
@@ -223,18 +255,19 @@ async fn test_os_resources_regression() {
223255
let mut maybe_deduct_inner =
224256
|total: ExecutionResources, selector: Selector| -> ExecutionResources {
225257
// We assume no inner calls have nested inner calls (all inner calls are leaves).
226-
if selector.is_calling_syscall() {
258+
(if selector.is_calling_syscall() {
227259
// TODO(Dori): Take opcodes (like blake) into account, instead of using the
228260
// vm_resources field.
229261
// TODO(Dori): Consider supporting memory-hole counting in the OsLogger. Until then,
230262
// we cannot subtract inner calls with positive memory-hole counts from the
231263
// OsLogger resources.
232264
let mut to_deduct = inner_calls_iter.next().unwrap().resources.vm_resources;
233265
to_deduct.n_memory_holes = 0;
234-
(&total - &to_deduct).filter_unused_builtins()
266+
&total - &to_deduct
235267
} else {
236268
total
237-
}
269+
})
270+
.filter_unused_builtins()
238271
};
239272
while let Some(syscall_trace) = syscalls_iter.next() {
240273
let selector = syscall_trace.get_selector();

0 commit comments

Comments
 (0)