11use std:: collections:: HashSet ;
22
33use blockifier:: blockifier_versioned_constants:: {
4+ RawStepGasCost ,
45 RawVersionedConstants ,
56 ResourcesParams ,
67 VariableCallDataFactor ,
@@ -31,7 +32,7 @@ use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS;
3132use crate :: utils:: get_class_hash_of_feature_contract;
3233
3334// TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls.
34- const UNMEASURABLE_SYSCALLS : [ Selector ; 27 ] = [
35+ const UNMEASURABLE_SYSCALLS : [ Selector ; 25 ] = [
3536 Selector :: DelegateCall ,
3637 Selector :: DelegateL1Handler ,
3738 Selector :: GetBlockNumber ,
@@ -41,8 +42,6 @@ const UNMEASURABLE_SYSCALLS: [Selector; 27] = [
4142 Selector :: GetSequencerAddress ,
4243 Selector :: GetTxInfo ,
4344 Selector :: GetTxSignature ,
44- Selector :: Keccak ,
45- Selector :: KeccakRound ,
4645 Selector :: Sha256ProcessBlock ,
4746 Selector :: LibraryCallL1Handler ,
4847 Selector :: ReplaceClass ,
@@ -61,7 +60,10 @@ const UNMEASURABLE_SYSCALLS: [Selector; 27] = [
6160 Selector :: StorageWrite ,
6261] ;
6362
64- const SYSCALLS_WITH_LINEAR_FACTOR : [ Selector ; 2 ] = [ Selector :: Deploy , Selector :: MetaTxV0 ] ;
63+ /// Keccak does not store the linear factor in the same entry in the versioned constants, but it
64+ /// does have a measurable linear factor stored under [Selector::KeccakRound].
65+ const SYSCALLS_WITH_LINEAR_FACTOR : [ Selector ; 3 ] =
66+ [ Selector :: Deploy , Selector :: Keccak , Selector :: MetaTxV0 ] ;
6567
6668/// Expected syscalls in the fee transfer call. Should be removed from the list of syscalls during
6769/// measurement iteration - only the syscalls called during __execute__ should be measured.
@@ -122,6 +124,9 @@ async fn test_fee_transfer_syscalls() {
122124async fn test_os_resources_regression ( ) {
123125 let os_resources_contract = FeatureContract :: OsResourcesTest ( RunnableCairo1 :: Casm ) ;
124126 let os_resources_class_hash = get_class_hash_of_feature_contract ( os_resources_contract) ;
127+ let version = StarknetVersion :: LATEST ;
128+ let mut raw_vc: RawVersionedConstants =
129+ serde_json:: from_str ( VersionedConstants :: json_str ( & version) . unwrap ( ) ) . unwrap ( ) ;
125130
126131 // Setup the test initial state and test builder.
127132 // Need to explicitly set up the state to be able to override the minimal sierra version for gas
@@ -253,7 +258,7 @@ async fn test_os_resources_regression() {
253258
254259 // If this if a syscall with a linear factor, the next syscall should be the linear cost.
255260 // Otherwise, this syscall has a constant cost.
256- let syscall_cost = if SYSCALLS_WITH_LINEAR_FACTOR . contains ( & selector) {
261+ if SYSCALLS_WITH_LINEAR_FACTOR . contains ( & selector) {
257262 let next_syscall_trace = syscalls_iter. next ( ) . unwrap ( ) ;
258263 assert_eq ! (
259264 selector,
@@ -267,16 +272,38 @@ async fn test_os_resources_regression() {
267272 - & next_inner_overhead)
268273 . filter_unused_builtins ( ) ;
269274 let linear_factor_resources = ( & next_resources - & resources) . filter_unused_builtins ( ) ;
270- VariableResourceParams :: WithFactor ( ResourcesParams {
271- constant : resources,
272- // Syscalls with a linear factor have an unscaled linear factor cost.
273- calldata_factor : VariableCallDataFactor :: Unscaled ( linear_factor_resources) ,
274- } )
275+ // Keccak is a special case - we store the linear cost as a separate syscall.
276+ if selector == Selector :: Keccak {
277+ // TODO(Dori): Currently, the Keccak base cost is enforced in the OS to equal the
278+ // syscall base cost. If and when this is no longer the case, no need to replace
279+ // `resources` (measured keccak base cost) with the syscall base cost, and no need
280+ // to recompute the linear factor.
281+ let RawStepGasCost { step_gas_cost : n_steps } =
282+ raw_vc. os_constants . syscall_base_gas_cost . clone ( ) ;
283+ let resources = ExecutionResources {
284+ n_steps : n_steps. 0 . try_into ( ) . unwrap ( ) ,
285+ ..Default :: default ( )
286+ } ;
287+ let linear_factor_resources =
288+ ( & next_resources - & resources) . filter_unused_builtins ( ) ;
289+ measurements. insert ( Selector :: Keccak , VariableResourceParams :: Constant ( resources) ) ;
290+ measurements. insert (
291+ Selector :: KeccakRound ,
292+ VariableResourceParams :: Constant ( linear_factor_resources) ,
293+ ) ;
294+ } else {
295+ measurements. insert (
296+ selector,
297+ VariableResourceParams :: WithFactor ( ResourcesParams {
298+ constant : resources,
299+ // Syscalls with a linear factor have an unscaled linear factor cost.
300+ calldata_factor : VariableCallDataFactor :: Unscaled ( linear_factor_resources) ,
301+ } ) ,
302+ ) ;
303+ }
275304 } else {
276- VariableResourceParams :: Constant ( resources)
277- } ;
278-
279- measurements. insert ( selector, syscall_cost) ;
305+ measurements. insert ( selector, VariableResourceParams :: Constant ( resources) ) ;
306+ }
280307 }
281308
282309 // Make sure we covered all syscalls we expect to.
@@ -297,9 +324,6 @@ async fn test_os_resources_regression() {
297324 ) ;
298325
299326 // Compare the measurements with the expected values on the latest VC.
300- let version = StarknetVersion :: LATEST ;
301- let mut raw_vc: RawVersionedConstants =
302- serde_json:: from_str ( VersionedConstants :: json_str ( & version) . unwrap ( ) ) . unwrap ( ) ;
303327 for ( syscall, resources) in measurements {
304328 raw_vc. os_resources . execute_syscalls . insert ( syscall, resources) ;
305329 }
0 commit comments