11use std:: collections:: { BTreeMap , HashSet } ;
22
3+ use blockifier:: execution:: contract_class:: { CompiledClassV1 , RunnableCompiledClass } ;
4+ use blockifier:: state:: state_api:: StateReader ;
5+ use blockifier:: state:: state_reader_and_contract_manager:: {
6+ FetchCompiledClasses ,
7+ StateReaderAndContractManager ,
8+ } ;
39use cairo_lang_starknet_classes:: casm_contract_class:: CasmContractClass ;
10+ use cairo_lang_utils:: bigint:: BigUintAsHex ;
11+ use cairo_vm:: types:: relocatable:: MaybeRelocatable ;
412use starknet_api:: core:: { ClassHash , CompiledClassHash } ;
5- use starknet_api :: deprecated_contract_class :: ContractClass ;
13+ use starknet_types_core :: felt :: Felt ;
614
715use crate :: errors:: ClassesProviderError ;
816
17+ /// Converts a `CompiledClassV1` to a `CasmContractClass` for OS execution.
18+ /// Note: Some fields are not preserved in `CompiledClassV1` and are set to default values:
19+ /// - `compiler_version`: Set to empty string
20+ /// - `hints`: Set to empty (OS doesn't use them from this struct for Cairo 1 contracts)
21+ /// - `pythonic_hints`: Set to None
22+ fn compiled_class_v1_to_casm ( class : & CompiledClassV1 ) -> CasmContractClass {
23+ // TODO(Aviv): Consider using dummy prime since it is not used in the OS.
24+ let prime = Felt :: prime ( ) ;
25+
26+ let bytecode: Vec < BigUintAsHex > = class
27+ . program
28+ . iter_data ( )
29+ . map ( |maybe_relocatable| match maybe_relocatable {
30+ MaybeRelocatable :: Int ( felt) => BigUintAsHex { value : felt. to_biguint ( ) } ,
31+ _ => panic ! ( "Expected all bytecode elements to be MaybeRelocatable::Int" ) ,
32+ } )
33+ . collect ( ) ;
34+
35+ CasmContractClass {
36+ prime,
37+ compiler_version : String :: new ( ) ,
38+ bytecode,
39+ bytecode_segment_lengths : Some ( class. bytecode_segment_felt_sizes ( ) . into ( ) ) ,
40+ hints : Vec :: new ( ) ,
41+ pythonic_hints : None ,
42+ entry_points_by_type : ( & class. entry_points_by_type ) . into ( ) ,
43+ }
44+ }
45+
946/// The classes required for a Starknet OS run.
10- /// Matches the fields in `StarknetOsInput` and `OsBlockInput` .
47+ /// Matches the fields in `StarknetOsInput`.
1148pub struct ClassesInput {
12- /// Deprecated (Cairo 0) contract classes.
13- /// Maps ClassHash to the contract class definition.
14- pub deprecated_compiled_classes : BTreeMap < ClassHash , ContractClass > ,
1549 /// Cairo 1+ contract classes (CASM).
1650 /// Maps CompiledClassHash to the CASM contract class definition.
1751 pub compiled_classes : BTreeMap < CompiledClassHash , CasmContractClass > ,
@@ -22,5 +56,48 @@ pub trait ClassesProvider {
2256 fn get_classes (
2357 & self ,
2458 executed_class_hashes : & HashSet < ClassHash > ,
25- ) -> Result < ClassesInput , ClassesProviderError > ;
59+ ) -> Result < ClassesInput , ClassesProviderError > {
60+ let mut compiled_classes = BTreeMap :: new ( ) ;
61+
62+ // TODO(Aviv): Parallelize the fetching of classes.
63+ for & class_hash in executed_class_hashes {
64+ let ( compiled_class_hash, casm) = self . fetch_class ( class_hash) ?;
65+ compiled_classes. insert ( compiled_class_hash, casm) ;
66+ }
67+ Ok ( ClassesInput { compiled_classes } )
68+ }
69+
70+ /// Fetches class by class hash.
71+ fn fetch_class (
72+ & self ,
73+ class_hash : ClassHash ,
74+ ) -> Result < ( CompiledClassHash , CasmContractClass ) , ClassesProviderError > ;
75+ }
76+
77+ impl < S : FetchCompiledClasses > ClassesProvider for StateReaderAndContractManager < S > {
78+ /// Fetch class from the state reader and contract manager.
79+ /// Returns error if the class is deprecated.
80+ fn fetch_class (
81+ & self ,
82+ class_hash : ClassHash ,
83+ ) -> Result < ( CompiledClassHash , CasmContractClass ) , ClassesProviderError > {
84+ let compiled_class = self . get_compiled_class ( class_hash) ?;
85+ // TODO(Aviv): Make sure that the state reader is not returning dummy compiled class hash.
86+ let compiled_class_hash = self . get_compiled_class_hash_v2 ( class_hash, & compiled_class) ?;
87+ match compiled_class {
88+ RunnableCompiledClass :: V0 ( _v0) => {
89+ Err ( ClassesProviderError :: DeprecatedContractError ( class_hash) )
90+ }
91+ RunnableCompiledClass :: V1 ( compiled_class_v1) => {
92+ let casm = compiled_class_v1_to_casm ( & compiled_class_v1) ;
93+ Ok ( ( compiled_class_hash, casm) )
94+ }
95+ #[ cfg( feature = "cairo_native" ) ]
96+ RunnableCompiledClass :: V1Native ( compiled_class_v1_native) => {
97+ let compiled_class_v1 = compiled_class_v1_native. casm ( ) ;
98+ let casm = compiled_class_v1_to_casm ( & compiled_class_v1) ;
99+ Ok ( ( compiled_class_hash, casm) )
100+ }
101+ }
102+ }
26103}
0 commit comments