Skip to content

Commit a281946

Browse files
starknet_os_flow_tests: measure OS linear syscall resources with many elements
Signed-off-by: Dori Medini <dori@starkware.co>
1 parent 3dd3a9e commit a281946

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ mod OsResourcesTestContract {
1111
const STABLE_EXTERNAL_ENTRY_POINT_SELECTOR: felt252 = selector!("external");
1212
const EXECUTE_FUNCTION_SELECTOR: felt252 = selector!("__execute__");
1313

14+
// Define a large input length for variable-length input syscalls.
15+
const LARGE_INPUT_LENGTH: usize = 100;
16+
1417
#[storage]
1518
struct Storage {}
1619

@@ -45,6 +48,12 @@ mod OsResourcesTestContract {
4548
return;
4649
}
4750

51+
// Define a large input for variable-length input syscalls.
52+
let mut large_input: Array<felt252> = array![LARGE_INPUT_LENGTH.into()];
53+
for _ in 0..LARGE_INPUT_LENGTH {
54+
large_input.append(1);
55+
}
56+
4857
// call_contract syscall — calls external function on stable contract.
4958
call_contract_syscall(
5059
stable_address, STABLE_EXTERNAL_ENTRY_POINT_SELECTOR, array![0].span(),
@@ -69,7 +78,7 @@ mod OsResourcesTestContract {
6978
meta_tx_v0_syscall(
7079
address: stable_address,
7180
entry_point_selector: EXECUTE_FUNCTION_SELECTOR,
72-
calldata: array![1, 0].span(),
81+
calldata: large_input.span(),
7382
signature: array![].span(),
7483
)
7584
.unwrap_syscall();
@@ -80,7 +89,7 @@ mod OsResourcesTestContract {
8089
// base (no calldata):
8190
deploy_syscall(stable_class_hash, 1, array![0].span(), true).unwrap_syscall();
8291
// linear factor (calldata len = 1):
83-
deploy_syscall(stable_class_hash, 1, array![1, 0].span(), true).unwrap_syscall();
92+
deploy_syscall(stable_class_hash, 1, large_input.span(), true).unwrap_syscall();
8493

8594
// emit event syscall.
8695
emit_event_syscall(array![5].span(), array![7].span()).unwrap_syscall();

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use blockifier::blockifier_versioned_constants::{
1111
use blockifier::context::BlockContext;
1212
use blockifier::execution::deprecated_syscalls::DeprecatedSyscallSelector as Selector;
1313
use blockifier::test_utils::dict_state_reader::DictStateReader;
14+
use blockifier::transaction::objects::ExecutionResourcesTraits;
1415
use blockifier_test_utils::cairo_versions::RunnableCairo1;
1516
use blockifier_test_utils::contracts::FeatureContract;
1617
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
@@ -77,21 +78,24 @@ const UNMEASURABLE_SYSCALLS: [Selector; 28] = [
7778

7879
/// Store a mapping from a linearly-charged syscall, with the number of "linear elements" in it's
7980
/// first measurement. For example, if we measure the base and linear costs of a [Selector::Deploy]
80-
/// by running:
81+
/// by measuring:
8182
/// ```
82-
/// deploy_syscall(stable_class_hash, 3, array![2, 0, 0].span(), true).unwrap_syscall();
83-
/// deploy_syscall(stable_class_hash, 3, array![3, 0, 0, 0].span(), true).unwrap_syscall();
83+
/// let deploy_from_zero = true;
84+
/// let M1 = COST(deploy_syscall(class_hash, salt, calldata: [0], deploy_from_zero));
85+
/// let M2 = COST(deploy_syscall(class_hash, salt, calldata: [100] + [0; 100], deploy_from_zero));
8486
/// ```
85-
/// ... then the linear factor is exactly the difference between the two measurements, because the
86-
/// second measurement has one more linear element (4) than the first measurement (3). However, the
87-
/// first call has 3 elements in it's calldata, so to compute the estimated cost of a zero-element
88-
/// call (base) we need to subtract three times the linear factor from the first measurement. In
89-
/// this case, the value in the mapping will be 3.
90-
/// The second call will always have one more linear element than the first call.
87+
/// then the base and linear parts can be computed by:
88+
/// ```
89+
/// let LINEAR = (M2 - M1) / 100;
90+
/// let BASE = M1 - LINEAR;
91+
/// ```
92+
/// The formula for `LINEAR` is simple, and note that we must subtract one `LINEAR` from `M1` to get
93+
/// `BASE` because the `M1` measurement has a single calldata element (length of calldata: 0).
9194
/// Note: Keccak does not store the linear factor in the same entry in the versioned constants, but
9295
/// it does have a measurable linear factor stored under [Selector::KeccakRound].
9396
static SYSCALLS_WITH_LINEAR_FACTOR: LazyLock<HashMap<Selector, usize>> =
9497
LazyLock::new(|| HashMap::from([(Selector::Deploy, 1), (Selector::MetaTxV0, 1)]));
98+
const LARGE_INPUT_LENGTH: usize = 100;
9599

96100
/// Expected syscalls in the fee transfer call. Should be removed from the list of syscalls during
97101
/// measurement iteration - only the syscalls called during __execute__ should be measured.
@@ -349,7 +353,9 @@ async fn test_os_resources_regression() {
349353
);
350354
let next_resources =
351355
maybe_deduct_inner(next_syscall_trace.get_resources().unwrap().clone(), selector);
352-
let linear_factor_resources = (&next_resources - &resources).filter_unused_builtins();
356+
let linear_factor_resources = (&next_resources - &resources)
357+
.div_ceil(LARGE_INPUT_LENGTH)
358+
.filter_unused_builtins();
353359

354360
// Linear factor is computed; deduct the linear overhead from the base cost to get the
355361
// real base cost.

0 commit comments

Comments
 (0)