@@ -14,6 +14,10 @@ use std::{
14
14
ffi:: { c_char, c_longlong, c_uchar, c_ulonglong, c_void, CStr , CString } ,
15
15
slice,
16
16
sync:: Arc ,
17
+ collections:: BTreeMap ,
18
+ fs:: File ,
19
+ io:: Read ,
20
+ path:: Path ,
17
21
} ;
18
22
19
23
use anyhow:: Result ;
@@ -63,6 +67,14 @@ use starknet_api::{
63
67
use starknet_types_core:: felt:: Felt ;
64
68
use std:: str:: FromStr ;
65
69
type StarkFelt = Felt ;
70
+ use once_cell:: sync:: Lazy ;
71
+ use anyhow:: Context ;
72
+
73
+ // Allow users to call CONSTRUCTOR entry point type which has fixed entry_point_felt "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194"
74
+ pub static CONSTRUCTOR_ENTRY_POINT_FELT : Lazy < StarkFelt > = Lazy :: new ( || {
75
+ StarkFelt :: from_hex ( "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194" )
76
+ . expect ( "Invalid hex string" )
77
+ } ) ;
66
78
67
79
extern "C" {
68
80
fn JunoReportError (
@@ -695,21 +707,48 @@ fn build_block_context(
695
707
696
708
#[ allow( static_mut_refs) ]
697
709
fn get_versioned_constants ( version : * const c_char ) -> VersionedConstants {
698
- if let Some ( constants ) = unsafe { & CUSTOM_VERSIONED_CONSTANTS } {
699
- return constants . clone ( ) ;
700
- }
710
+ let starknet_version = unsafe { CStr :: from_ptr ( version ) } . to_str ( )
711
+ . ok ( )
712
+ . and_then ( |version_str| StarknetVersion :: try_from ( version_str ) . ok ( ) ) ;
701
713
702
- let version_str = unsafe { CStr :: from_ptr ( version) } . to_str ( ) ;
714
+ if let ( Some ( custom_constants) , Some ( version) ) = ( unsafe { & CUSTOM_VERSIONED_CONSTANTS } , starknet_version) {
715
+ if let Some ( constants) = custom_constants. 0 . get ( & version) {
716
+ return constants. clone ( ) ;
717
+ }
718
+ }
703
719
704
- version_str
705
- . ok ( )
706
- . and_then ( |version| StarknetVersion :: try_from ( version) . ok ( ) )
720
+ starknet_version
707
721
. and_then ( |version| VersionedConstants :: get ( & version) . ok ( ) )
708
722
. unwrap_or ( VersionedConstants :: latest_constants ( ) )
709
723
. to_owned ( )
710
724
}
711
725
712
- static mut CUSTOM_VERSIONED_CONSTANTS : Option < VersionedConstants > = None ;
726
+ #[ derive( Debug ) ]
727
+ pub struct VersionedConstantsMap ( pub BTreeMap < StarknetVersion , VersionedConstants > ) ;
728
+
729
+ impl VersionedConstantsMap {
730
+ pub fn from_file ( version_with_path : BTreeMap < String , String > ) -> Result < Self > {
731
+ let mut result = BTreeMap :: new ( ) ;
732
+
733
+ for ( version, path) in version_with_path {
734
+ let mut file = File :: open ( Path :: new ( & path) ) . with_context ( || format ! ( "Failed to open file: {}" , path) ) ?;
735
+
736
+ let mut contents = String :: new ( ) ;
737
+ file. read_to_string ( & mut contents) . with_context ( || format ! ( "Failed to read contents of file: {}" , path) ) ?;
738
+
739
+ let constants: VersionedConstants =
740
+ serde_json:: from_str ( & contents) . with_context ( || format ! ( "Failed to parse JSON in file: {}" , path) ) ?;
741
+
742
+ let parsed_version = StarknetVersion :: try_from ( version. as_str ( ) ) . with_context ( || format ! ( "Failed to parse version string: {}" , version) ) ?;
743
+
744
+ result. insert ( parsed_version, constants) ;
745
+ }
746
+
747
+ Ok ( VersionedConstantsMap ( result) )
748
+ }
749
+ }
750
+
751
+ static mut CUSTOM_VERSIONED_CONSTANTS : Option < VersionedConstantsMap > = None ;
713
752
714
753
#[ no_mangle]
715
754
#[ allow( clippy:: not_unsafe_ptr_arg_deref) ]
@@ -725,14 +764,23 @@ pub extern "C" fn setVersionedConstants(json_bytes: *const c_char) -> *const c_c
725
764
}
726
765
} ;
727
766
728
- match serde_json:: from_str :: < VersionedConstants > ( json_str) {
729
- Ok ( parsed) => unsafe {
730
- CUSTOM_VERSIONED_CONSTANTS = Some ( parsed) ;
731
- CString :: new ( "" ) . unwrap ( ) . into_raw ( ) // No error, return an empty string
732
- } ,
733
- Err ( e) => CString :: new ( format ! ( "Failed to parse JSON: {}" , e) )
734
- . unwrap ( )
735
- . into_raw ( ) ,
767
+ let versioned_constants_files_paths: Result < BTreeMap < String , String > , _ > = serde_json:: from_str ( json_str) ;
768
+ if let Ok ( paths) = versioned_constants_files_paths {
769
+ match VersionedConstantsMap :: from_file ( paths) {
770
+ Ok ( custom_constants) => {
771
+ unsafe {
772
+ CUSTOM_VERSIONED_CONSTANTS = Some ( custom_constants) ;
773
+ return CString :: new ( "" ) . unwrap ( ) . into_raw ( ) ;
774
+ }
775
+ } ,
776
+ Err ( e) => {
777
+ return CString :: new ( format ! ( "Failed to load versioned constants from paths: {}" , e) )
778
+ . unwrap ( )
779
+ . into_raw ( ) ;
780
+ }
781
+ }
782
+ } else {
783
+ return CString :: new ( "Failed to parse JSON" ) . unwrap ( ) . into_raw ( ) ;
736
784
}
737
785
}
738
786
0 commit comments