Skip to content

Commit a2d9a38

Browse files
test(apollo_starknet_os_program): test all non-deprecated syscalls have gas costs
1 parent af1bae1 commit a2d9a38

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
@@ -27,6 +27,8 @@ serde_json.workspace = true
2727

2828
[dev-dependencies]
2929
apollo_infra_utils = { workspace = true, features = ["testing"] }
30-
blockifier.workspace = true
30+
blockifier = { workspace = true, features = ["testing"] }
3131
starknet-types-core.workspace = true
3232
starknet_api.workspace = true
33+
strum.workspace = true
34+
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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ pub mod hint_processor;
3737
pub type DeprecatedSyscallResult<T> = Result<T, DeprecatedSyscallExecutionError>;
3838
pub type WriteResponseResult = DeprecatedSyscallExecutorBaseResult<()>;
3939

40-
#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize))]
40+
#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize, strum_macros::AsRefStr))]
41+
#[cfg_attr(any(test, feature = "testing"), strum(serialize_all = "SCREAMING_SNAKE_CASE"))]
4142
#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq)]
4243
pub enum DeprecatedSyscallSelector {
4344
CallContract,

0 commit comments

Comments
 (0)