Skip to content

Commit b78e7ca

Browse files
test(apollo_starknet_os_program): test all non-deprecated syscalls have gas costs
1 parent 185915b commit b78e7ca

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_starknet_os_program/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ serde_json.workspace = true
2222

2323
[dev-dependencies]
2424
apollo_infra_utils = { workspace = true, features = ["testing"] }
25-
blockifier.workspace = true
25+
blockifier = { workspace = true, features = ["testing"] }
2626
starknet-types-core.workspace = true
2727
starknet_api.workspace = true
28+
strum.workspace = true
29+
strum_macros.workspace = true

crates/apollo_starknet_os_program/src/constants_test.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use blockifier::blockifier_versioned_constants::{OsConstants, VersionedConstants
66
use blockifier::execution::syscalls::vm_syscall_utils::SyscallSelector;
77
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
88
use starknet_types_core::felt::Felt;
9+
use strum::IntoEnumIterator;
910

1011
const CONSTANTS_CONTENTS: &str = include_str!("cairo/starkware/starknet/core/os/constants.cairo");
1112

@@ -232,3 +233,41 @@ fn test_os_constants() {
232233
);
233234
}
234235
}
236+
237+
/// Test that all syscalls have a gas cost, and syscalls with linear factors have both costs.
238+
#[test]
239+
fn test_all_syscall_have_gas_costs() {
240+
let os_constants_template =
241+
include_str!("cairo/starkware/starknet/core/os/constants_template.txt");
242+
let syscall_costs = &VersionedConstants::latest_constants().os_constants.gas_costs.syscalls;
243+
for selector in SyscallSelector::iter() {
244+
let Ok(cost) = syscall_costs.get_syscall_gas_cost(&selector) else {
245+
// We don't care about deprecated syscalls (without gas costs).
246+
continue;
247+
};
248+
249+
let selector_str: &str = match selector {
250+
// Keccak round is a special case, since it has a different name in the template.
251+
SyscallSelector::KeccakRound => "KECCAK_ROUND_COST",
252+
_ => selector.as_ref(),
253+
};
254+
let gas_cost_str = format!("{selector_str}_GAS_COST = {{{selector_str}_GAS_COST}}");
255+
256+
// Base cost always exists.
257+
assert!(
258+
os_constants_template.contains(&gas_cost_str),
259+
"Syscall {selector_str} should have a base gas cost."
260+
);
261+
// If the syscall has a linear factor check for it.
262+
if cost.linear_syscall_cost() != 0 {
263+
let linear_factor_str = format!(
264+
"{selector_str}_CALLDATA_FACTOR_GAS_COST = \
265+
{{{selector_str}_CALLDATA_FACTOR_GAS_COST}}"
266+
);
267+
assert!(
268+
os_constants_template.contains(&linear_factor_str),
269+
"Syscall {selector_str} should have a linear gas cost."
270+
);
271+
}
272+
}
273+
}

crates/blockifier/src/execution/deprecated_syscalls/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ pub mod hint_processor;
3333
pub type DeprecatedSyscallResult<T> = Result<T, DeprecatedSyscallExecutionError>;
3434
pub type WriteResponseResult = DeprecatedSyscallResult<()>;
3535

36-
#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize))]
36+
#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize, strum_macros::AsRefStr))]
37+
#[cfg_attr(any(test, feature = "testing"), strum(serialize_all = "SCREAMING_SNAKE_CASE"))]
3738
#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq)]
3839
pub enum DeprecatedSyscallSelector {
3940
CallContract,

0 commit comments

Comments
 (0)