Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
}
},
"EmitEvent": {
"n_steps": 61,
"n_steps": 47,
"n_memory_holes": 0,
"builtin_instance_counter": {
"range_check_builtin": 1
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
}
51 changes: 42 additions & 9 deletions crates/starknet_os_flow_tests/src/os_resources_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
let mut syscalls_iter = syscall_traces
.iter()
.filter(|syscall_trace| !UNMEASURABLE_SYSCALLS.contains(&syscall_trace.get_selector()));
Expand All @@ -223,18 +255,19 @@ 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,
// we cannot subtract inner calls with positive memory-hole counts from the
// 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();
Expand Down
Loading