From a2d9a383be13023f5f20ae889c0173f00331703e Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Wed, 21 May 2025 15:58:26 +0300 Subject: [PATCH] test(apollo_starknet_os_program): test all non-deprecated syscalls have gas costs --- Cargo.lock | 2 + crates/apollo_starknet_os_program/Cargo.toml | 4 +- .../src/constants_test.rs | 39 +++++++++++++++++++ .../src/execution/deprecated_syscalls/mod.rs | 3 +- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cbf011a2e95..3918ba26257 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1929,6 +1929,8 @@ dependencies = [ "serde_json", "starknet-types-core", "starknet_api", + "strum 0.25.0", + "strum_macros 0.25.3", "thiserror 1.0.69", ] diff --git a/crates/apollo_starknet_os_program/Cargo.toml b/crates/apollo_starknet_os_program/Cargo.toml index 89f363497e7..2be4fff9e85 100644 --- a/crates/apollo_starknet_os_program/Cargo.toml +++ b/crates/apollo_starknet_os_program/Cargo.toml @@ -27,6 +27,8 @@ serde_json.workspace = true [dev-dependencies] apollo_infra_utils = { workspace = true, features = ["testing"] } -blockifier.workspace = true +blockifier = { workspace = true, features = ["testing"] } starknet-types-core.workspace = true starknet_api.workspace = true +strum.workspace = true +strum_macros.workspace = true diff --git a/crates/apollo_starknet_os_program/src/constants_test.rs b/crates/apollo_starknet_os_program/src/constants_test.rs index e239dc11d78..2305d08109b 100644 --- a/crates/apollo_starknet_os_program/src/constants_test.rs +++ b/crates/apollo_starknet_os_program/src/constants_test.rs @@ -6,6 +6,7 @@ use blockifier::blockifier_versioned_constants::{OsConstants, VersionedConstants use blockifier::execution::syscalls::vm_syscall_utils::SyscallSelector; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector}; use starknet_types_core::felt::Felt; +use strum::IntoEnumIterator; const CONSTANTS_CONTENTS: &str = include_str!("cairo/starkware/starknet/core/os/constants.cairo"); @@ -233,3 +234,41 @@ fn test_os_constants() { ); } } + +/// Test that all syscalls have a gas cost, and syscalls with linear factors have both costs. +#[test] +fn test_all_syscall_have_gas_costs() { + let os_constants_template = + include_str!("cairo/starkware/starknet/core/os/constants_template.txt"); + let syscall_costs = &VersionedConstants::latest_constants().os_constants.gas_costs.syscalls; + for selector in SyscallSelector::iter() { + let Ok(cost) = syscall_costs.get_syscall_gas_cost(&selector) else { + // We don't care about deprecated syscalls (without gas costs). + continue; + }; + + let selector_str: &str = match selector { + // Keccak round is a special case, since it has a different name in the template. + SyscallSelector::KeccakRound => "KECCAK_ROUND_COST", + _ => selector.as_ref(), + }; + let gas_cost_str = format!("{selector_str}_GAS_COST = {{{selector_str}_GAS_COST}}"); + + // Base cost always exists. + assert!( + os_constants_template.contains(&gas_cost_str), + "Syscall {selector_str} should have a base gas cost." + ); + // If the syscall has a linear factor check for it. + if cost.linear_syscall_cost() != 0 { + let linear_factor_str = format!( + "{selector_str}_CALLDATA_FACTOR_GAS_COST = \ + {{{selector_str}_CALLDATA_FACTOR_GAS_COST}}" + ); + assert!( + os_constants_template.contains(&linear_factor_str), + "Syscall {selector_str} should have a linear gas cost." + ); + } + } +} diff --git a/crates/blockifier/src/execution/deprecated_syscalls/mod.rs b/crates/blockifier/src/execution/deprecated_syscalls/mod.rs index be8c220a71c..a6d629107a5 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/mod.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/mod.rs @@ -37,7 +37,8 @@ pub mod hint_processor; pub type DeprecatedSyscallResult = Result; pub type WriteResponseResult = DeprecatedSyscallExecutorBaseResult<()>; -#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize))] +#[cfg_attr(any(test, feature = "testing"), derive(serde::Serialize, strum_macros::AsRefStr))] +#[cfg_attr(any(test, feature = "testing"), strum(serialize_all = "SCREAMING_SNAKE_CASE"))] #[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq)] pub enum DeprecatedSyscallSelector { CallContract,