Skip to content

Commit 1b6cf5e

Browse files
starknet_os: os resources test - remove all fee transfer syscalls
1 parent fd9c8c3 commit 1b6cf5e

2 files changed

Lines changed: 72 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ mod OsResourcesTestContract {
3939
fn __execute__(
4040
ref self: ContractState, stable_class_hash: ClassHash, stable_address: ContractAddress,
4141
) {
42+
// Skip everything if inputs are zero.
43+
if stable_class_hash.is_zero() && stable_address.is_zero() {
44+
return;
45+
}
46+
4247
// call_contract syscall — calls external function on stable contract.
4348
call_contract_syscall(
4449
stable_address, STABLE_EXTERNAL_ENTRY_POINT_SELECTOR, array![0].span(),

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,62 @@ const UNMEASURABLE_SYSCALLS: [Selector; 31] = [
9696
static SYSCALLS_WITH_LINEAR_FACTOR: LazyLock<HashMap<Selector, usize>> =
9797
LazyLock::new(|| HashMap::from([(Selector::Deploy, 1), (Selector::MetaTxV0, 1)]));
9898

99+
/// Expected syscalls in the fee transfer call. Should be removed from the list of syscalls during
100+
/// measurement iteration - only the syscalls called during __execute__ should be measured.
101+
const FEE_TRANSFER_SYSCALLS: [Selector; 10] = [
102+
Selector::GetExecutionInfo,
103+
Selector::StorageRead,
104+
Selector::StorageRead,
105+
Selector::StorageWrite,
106+
Selector::StorageWrite,
107+
Selector::StorageRead,
108+
Selector::StorageRead,
109+
Selector::StorageWrite,
110+
Selector::StorageWrite,
111+
Selector::EmitEvent,
112+
];
113+
114+
/// Regression test for the list of syscalls called during the fee transfer phase of a transaction.
115+
#[tokio::test]
116+
async fn test_fee_transfer_syscalls() {
117+
let os_resources_contract = FeatureContract::OsResourcesTest(RunnableCairo1::Casm);
118+
let (mut builder, [os_resources_contract_address]) =
119+
TestBuilder::create_standard([(os_resources_contract, calldata![])]).await;
120+
121+
// Fund the contract - it will be used as the account.
122+
// Then, move on to the next block, so the syscall-measurement tx is in it's own block.
123+
builder.add_fund_address_tx_with_default_amount(os_resources_contract_address);
124+
125+
// Invoke from the OS resources contract, with zeros as calldata, to make the __execute__ do
126+
// nothing. All resulting events should be from the fee transfer call.
127+
builder.add_invoke_tx(
128+
InvokeTransaction::create(
129+
invoke_tx(invoke_tx_args! {
130+
sender_address: os_resources_contract_address,
131+
calldata: calldata![Felt::ZERO, Felt::ZERO],
132+
resource_bounds: *NON_TRIVIAL_RESOURCE_BOUNDS,
133+
}),
134+
&builder.chain_id(),
135+
)
136+
.unwrap(),
137+
None,
138+
None,
139+
);
140+
141+
// Build, run, and get the syscalls list.
142+
let test_output = builder.build_and_run().await;
143+
let syscalls = test_output
144+
.runner_output
145+
.txs_trace
146+
.last()
147+
.unwrap()
148+
.get_syscalls()
149+
.iter()
150+
.map(|syscall_trace| syscall_trace.get_selector())
151+
.collect::<Vec<_>>();
152+
assert_eq!(syscalls, FEE_TRANSFER_SYSCALLS.to_vec());
153+
}
154+
99155
/// Measure the OS overhead for each syscall, and compare the results with the latest VC.
100156
///
101157
/// This test relies on the [starknet_os::hint_processor::os_logger::OsLogger] to capture the
@@ -231,25 +287,23 @@ async fn test_os_resources_regression() {
231287
let mut inner_calls_iter = inner_calls.into_iter();
232288

233289
// 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));
290+
// Remove the fee transfer syscalls from the list by splitting the iterator into two. The second
291+
// part is the last `FEE_TRANSFER_SYSCALLS.len()` syscalls, and the first part should be the
292+
// rest.
293+
let all_syscalls = test_output.runner_output.txs_trace.last().unwrap().get_syscalls().clone();
294+
let (syscall_traces, fee_transfer_syscall_traces) =
295+
all_syscalls.split_at(all_syscalls.len() - FEE_TRANSFER_SYSCALLS.len());
239296
assert_eq!(
240-
syscall_traces
297+
fee_transfer_syscall_traces
241298
.iter()
242-
.filter(|syscall_trace| syscall_trace.get_selector() == Selector::EmitEvent)
243-
.count(),
244-
2
299+
.map(|syscall_trace| syscall_trace.get_selector())
300+
.collect::<Vec<_>>(),
301+
FEE_TRANSFER_SYSCALLS.to_vec()
245302
);
246-
assert_eq!(syscall_traces.pop().unwrap().get_selector(), Selector::EmitEvent);
247303

248304
// Measure each syscall overhead. If the syscall incurs an inner call, subtract the inner call
249305
// overhead.
250-
let mut syscalls_iter = syscall_traces
251-
.iter()
252-
.filter(|syscall_trace| !UNMEASURABLE_SYSCALLS.contains(&syscall_trace.get_selector()));
306+
let mut syscalls_iter = syscall_traces.iter();
253307
let mut measurements: IndexMap<Selector, VariableResourceParams> = IndexMap::new();
254308
// If the syscall incurs an inner call, subtract the inner call overhead.
255309
let mut maybe_deduct_inner =

0 commit comments

Comments
 (0)