11use std:: convert:: From ;
22use std:: fmt;
3- use std:: sync:: Arc ;
3+ use std:: sync:: { Arc , LazyLock , Mutex } ;
44
55use ark_ec:: short_weierstrass:: { Affine , Projective , SWCurveConfig } ;
66use ark_ff:: { BigInt , PrimeField } ;
77use cairo_native:: starknet:: {
8- BlockInfo ,
9- ExecutionInfo ,
10- ExecutionInfoV2 ,
11- Secp256k1Point ,
12- Secp256r1Point ,
13- StarknetSyscallHandler ,
14- SyscallResult ,
15- TxInfo ,
16- TxV2Info ,
17- U256 ,
8+ BlockInfo , ExecutionInfo , ExecutionInfoV2 , Secp256k1Point , Secp256r1Point ,
9+ StarknetSyscallHandler , SyscallResult , TxInfo , TxV2Info , U256 ,
1810} ;
1911use num_bigint:: BigUint ;
2012use starknet_api:: contract_class:: EntryPointType ;
@@ -28,15 +20,12 @@ use starknet_types_core::felt::Felt;
2820use crate :: execution:: call_info:: { MessageToL1 , Retdata } ;
2921use crate :: execution:: common_hints:: ExecutionMode ;
3022use crate :: execution:: entry_point:: {
31- CallEntryPoint ,
32- CallType ,
33- EntryPointExecutionContext ,
34- ExecutableCallEntryPoint ,
23+ CallEntryPoint , CallType , EntryPointExecutionContext , ExecutableCallEntryPoint ,
3524} ;
3625use crate :: execution:: errors:: EntryPointExecutionError ;
3726use crate :: execution:: native:: utils:: { calculate_resource_bounds, default_tx_v2_info} ;
3827use crate :: execution:: secp;
39- use crate :: execution:: syscalls:: hint_processor:: { SyscallExecutionError , OUT_OF_GAS_ERROR } ;
28+ use crate :: execution:: syscalls:: hint_processor:: { OUT_OF_GAS_ERROR , SyscallExecutionError } ;
4029use crate :: execution:: syscalls:: syscall_base:: SyscallHandlerBase ;
4130use crate :: state:: state_api:: State ;
4231use crate :: transaction:: objects:: TransactionInfo ;
@@ -45,6 +34,38 @@ use crate::versioned_constants::GasCosts;
4534
4635pub const CALL_CONTRACT_SELECTOR_NAME : & str = "call_contract" ;
4736pub const LIBRARY_CALL_SELECTOR_NAME : & str = "library_call" ;
37+
38+ pub static SYSCALL_COUNTER : LazyLock < Mutex < SyscallCounts > > = LazyLock :: new ( || Mutex :: new ( SyscallCounts :: default ( ) ) ) ;
39+
40+ #[ derive( Default , Clone , Copy ) ]
41+ pub struct SyscallCounts {
42+ pub get_block_hash : u64 ,
43+ pub get_execution_info : u64 ,
44+ pub get_execution_info_v2 : u64 ,
45+ pub deploy : u64 ,
46+ pub replace_class : u64 ,
47+ pub library_call : u64 ,
48+ pub call_contract : u64 ,
49+ pub storage_read : u64 ,
50+ pub storage_write : u64 ,
51+ pub emit_event : u64 ,
52+ pub send_message_to_l1 : u64 ,
53+ pub keccak : u64 ,
54+ pub secp256k1_new : u64 ,
55+ pub secp256k1_add : u64 ,
56+ pub secp256k1_mul : u64 ,
57+ pub secp256k1_get_point_from_x : u64 ,
58+ pub secp256k1_get_xy : u64 ,
59+ pub secp256r1_new : u64 ,
60+ pub secp256r1_add : u64 ,
61+ pub secp256r1_mul : u64 ,
62+ pub secp256r1_get_point_from_x : u64 ,
63+ pub secp256r1_get_xy : u64 ,
64+ pub sha256_process_block : u64 ,
65+ pub get_class_hash_at : u64 ,
66+ pub meta_tx_v0 : u64 ,
67+ }
68+
4869pub struct NativeSyscallHandler < ' state > {
4970 pub base : Box < SyscallHandlerBase < ' state > > ,
5071
@@ -239,6 +260,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
239260 block_number : u64 ,
240261 remaining_gas : & mut u64 ,
241262 ) -> SyscallResult < Felt > {
263+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . get_block_hash += 1 ;
264+
242265 self . pre_execute_syscall (
243266 remaining_gas,
244267 self . gas_costs ( ) . syscalls . get_block_hash . base_syscall_cost ( ) ,
@@ -251,6 +274,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
251274 }
252275
253276 fn get_execution_info ( & mut self , remaining_gas : & mut u64 ) -> SyscallResult < ExecutionInfo > {
277+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . get_execution_info += 1 ;
278+
254279 self . pre_execute_syscall (
255280 remaining_gas,
256281 self . gas_costs ( ) . syscalls . get_execution_info . base_syscall_cost ( ) ,
@@ -270,6 +295,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
270295 contract_address : Felt ,
271296 remaining_gas : & mut u64 ,
272297 ) -> SyscallResult < Felt > {
298+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . get_class_hash_at += 1 ;
299+
273300 self . pre_execute_syscall (
274301 remaining_gas,
275302 self . gas_costs ( ) . syscalls . get_class_hash_at . base_syscall_cost ( ) ,
@@ -285,6 +312,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
285312 }
286313
287314 fn get_execution_info_v2 ( & mut self , remaining_gas : & mut u64 ) -> SyscallResult < ExecutionInfoV2 > {
315+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . get_execution_info_v2 += 1 ;
316+
288317 self . pre_execute_syscall (
289318 remaining_gas,
290319 self . gas_costs ( ) . syscalls . get_execution_info . base_syscall_cost ( ) ,
@@ -307,6 +336,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
307336 deploy_from_zero : bool ,
308337 remaining_gas : & mut u64 ,
309338 ) -> SyscallResult < ( Felt , Vec < Felt > ) > {
339+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . deploy += 1 ;
310340 // The cost of deploying a contract is the base cost plus the linear cost of the calldata
311341 // len.
312342 let total_gas_cost =
@@ -330,6 +360,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
330360 Ok ( ( Felt :: from ( deployed_contract_address) , constructor_retdata) )
331361 }
332362 fn replace_class ( & mut self , class_hash : Felt , remaining_gas : & mut u64 ) -> SyscallResult < ( ) > {
363+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . replace_class += 1 ;
333364 self . pre_execute_syscall (
334365 remaining_gas,
335366 self . gas_costs ( ) . syscalls . replace_class . base_syscall_cost ( ) ,
@@ -348,6 +379,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
348379 calldata : & [ Felt ] ,
349380 remaining_gas : & mut u64 ,
350381 ) -> SyscallResult < Vec < Felt > > {
382+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . library_call += 1 ;
351383 self . pre_execute_syscall (
352384 remaining_gas,
353385 self . gas_costs ( ) . syscalls . library_call . base_syscall_cost ( ) ,
@@ -392,6 +424,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
392424 calldata : & [ Felt ] ,
393425 remaining_gas : & mut u64 ,
394426 ) -> SyscallResult < Vec < Felt > > {
427+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . call_contract += 1 ;
428+
395429 self . pre_execute_syscall (
396430 remaining_gas,
397431 self . gas_costs ( ) . syscalls . call_contract . base_syscall_cost ( ) ,
@@ -448,6 +482,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
448482 address : Felt ,
449483 remaining_gas : & mut u64 ,
450484 ) -> SyscallResult < Felt > {
485+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . storage_read += 1 ;
486+
451487 self . pre_execute_syscall (
452488 remaining_gas,
453489 self . gas_costs ( ) . syscalls . storage_read . base_syscall_cost ( ) ,
@@ -473,6 +509,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
473509 value : Felt ,
474510 remaining_gas : & mut u64 ,
475511 ) -> SyscallResult < ( ) > {
512+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . storage_write += 1 ;
513+
476514 self . pre_execute_syscall (
477515 remaining_gas,
478516 self . gas_costs ( ) . syscalls . storage_write . base_syscall_cost ( ) ,
@@ -497,6 +535,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
497535 data : & [ Felt ] ,
498536 remaining_gas : & mut u64 ,
499537 ) -> SyscallResult < ( ) > {
538+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . emit_event += 1 ;
539+
500540 self . pre_execute_syscall (
501541 remaining_gas,
502542 self . gas_costs ( ) . syscalls . emit_event . base_syscall_cost ( ) ,
@@ -517,6 +557,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
517557 payload : & [ Felt ] ,
518558 remaining_gas : & mut u64 ,
519559 ) -> SyscallResult < ( ) > {
560+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . send_message_to_l1 += 1 ;
561+
520562 self . pre_execute_syscall (
521563 remaining_gas,
522564 self . gas_costs ( ) . syscalls . send_message_to_l1 . base_syscall_cost ( ) ,
@@ -530,6 +572,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
530572 }
531573
532574 fn keccak ( & mut self , input : & [ u64 ] , remaining_gas : & mut u64 ) -> SyscallResult < U256 > {
575+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . keccak += 1 ;
576+
533577 self . pre_execute_syscall (
534578 remaining_gas,
535579 self . gas_costs ( ) . syscalls . keccak . base_syscall_cost ( ) ,
@@ -550,6 +594,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
550594 y : U256 ,
551595 remaining_gas : & mut u64 ,
552596 ) -> SyscallResult < Option < Secp256k1Point > > {
597+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256k1_new += 1 ;
598+
553599 self . pre_execute_syscall (
554600 remaining_gas,
555601 self . gas_costs ( ) . syscalls . secp256k1_new . base_syscall_cost ( ) ,
@@ -566,6 +612,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
566612 p1 : Secp256k1Point ,
567613 remaining_gas : & mut u64 ,
568614 ) -> SyscallResult < Secp256k1Point > {
615+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256k1_add += 1 ;
569616 self . pre_execute_syscall (
570617 remaining_gas,
571618 self . gas_costs ( ) . syscalls . secp256k1_add . base_syscall_cost ( ) ,
@@ -580,6 +627,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
580627 m : U256 ,
581628 remaining_gas : & mut u64 ,
582629 ) -> SyscallResult < Secp256k1Point > {
630+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256k1_mul += 1 ;
631+
583632 self . pre_execute_syscall (
584633 remaining_gas,
585634 self . gas_costs ( ) . syscalls . secp256k1_mul . base_syscall_cost ( ) ,
@@ -594,6 +643,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
594643 y_parity : bool ,
595644 remaining_gas : & mut u64 ,
596645 ) -> SyscallResult < Option < Secp256k1Point > > {
646+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256k1_get_point_from_x += 1 ;
647+
597648 self . pre_execute_syscall (
598649 remaining_gas,
599650 self . gas_costs ( ) . syscalls . secp256k1_get_point_from_x . base_syscall_cost ( ) ,
@@ -609,6 +660,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
609660 p : Secp256k1Point ,
610661 remaining_gas : & mut u64 ,
611662 ) -> SyscallResult < ( U256 , U256 ) > {
663+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256k1_get_xy += 1 ;
612664 self . pre_execute_syscall (
613665 remaining_gas,
614666 self . gas_costs ( ) . syscalls . secp256k1_get_xy . base_syscall_cost ( ) ,
@@ -623,6 +675,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
623675 y : U256 ,
624676 remaining_gas : & mut u64 ,
625677 ) -> SyscallResult < Option < Secp256r1Point > > {
678+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256r1_new += 1 ;
679+
626680 self . pre_execute_syscall (
627681 remaining_gas,
628682 self . gas_costs ( ) . syscalls . secp256r1_new . base_syscall_cost ( ) ,
@@ -639,6 +693,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
639693 p1 : Secp256r1Point ,
640694 remaining_gas : & mut u64 ,
641695 ) -> SyscallResult < Secp256r1Point > {
696+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256r1_add += 1 ;
697+
642698 self . pre_execute_syscall (
643699 remaining_gas,
644700 self . gas_costs ( ) . syscalls . secp256r1_add . base_syscall_cost ( ) ,
@@ -652,6 +708,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
652708 m : U256 ,
653709 remaining_gas : & mut u64 ,
654710 ) -> SyscallResult < Secp256r1Point > {
711+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256r1_mul += 1 ;
712+
655713 self . pre_execute_syscall (
656714 remaining_gas,
657715 self . gas_costs ( ) . syscalls . secp256r1_mul . base_syscall_cost ( ) ,
@@ -666,6 +724,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
666724 y_parity : bool ,
667725 remaining_gas : & mut u64 ,
668726 ) -> SyscallResult < Option < Secp256r1Point > > {
727+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256r1_get_point_from_x += 1 ;
728+
669729 self . pre_execute_syscall (
670730 remaining_gas,
671731 self . gas_costs ( ) . syscalls . secp256r1_get_point_from_x . base_syscall_cost ( ) ,
@@ -681,6 +741,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
681741 p : Secp256r1Point ,
682742 remaining_gas : & mut u64 ,
683743 ) -> SyscallResult < ( U256 , U256 ) > {
744+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . secp256r1_get_xy += 1 ;
745+
684746 self . pre_execute_syscall (
685747 remaining_gas,
686748 self . gas_costs ( ) . syscalls . secp256r1_get_xy . base_syscall_cost ( ) ,
@@ -695,6 +757,8 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
695757 current_block : & [ u32 ; 16 ] ,
696758 remaining_gas : & mut u64 ,
697759 ) -> SyscallResult < ( ) > {
760+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . sha256_process_block += 1 ;
761+
698762 self . pre_execute_syscall (
699763 remaining_gas,
700764 self . gas_costs ( ) . syscalls . sha256_process_block . base_syscall_cost ( ) ,
@@ -720,6 +784,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> {
720784 signature : & [ Felt ] ,
721785 remaining_gas : & mut u64 ,
722786 ) -> SyscallResult < Vec < Felt > > {
787+ SYSCALL_COUNTER . lock ( ) . unwrap ( ) . meta_tx_v0 += 1 ;
723788 todo ! (
724789 "implement meta_tx_v0 {:?}" ,
725790 ( address, entry_point_selector, calldata, signature, remaining_gas)
0 commit comments