@@ -22,6 +22,7 @@ use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
2222use starknet_api:: deprecated_contract_class:: ContractClass as DeprecatedClass ;
2323use starknet_api:: state:: { SierraContractClass , StateNumber , StorageKey } ;
2424use starknet_types_core:: felt:: Felt ;
25+ use tracing:: error;
2526
2627#[ cfg( test) ]
2728#[ path = "apollo_state_test.rs" ]
@@ -114,9 +115,14 @@ impl ApolloReader {
114115 . map_err ( |error| StateError :: StateReadError ( error. to_string ( ) ) )
115116 }
116117
118+ // TODO(Arni): Refactor this function so it is clear we use it only when the class reader is not
119+ // set.
117120 /// Returns a V1 contract with Sierra if V1 contract is found, or a V0 contract without Sierra
118121 /// if a V1 contract is not found, or an `Error` otherwise.
119- fn get_compiled_class_from_db ( & self , class_hash : ClassHash ) -> StateResult < CompiledClasses > {
122+ fn get_compiled_classes_from_storage (
123+ & self ,
124+ class_hash : ClassHash ,
125+ ) -> StateResult < CompiledClasses > {
120126 if self . is_declared ( class_hash) ? {
121127 // Cairo 1.
122128 let ( casm_compiled_class, sierra) = self . read_casm_and_sierra ( class_hash) ?;
@@ -137,6 +143,40 @@ impl ApolloReader {
137143 }
138144 }
139145
146+ fn get_compiled_classes_from_class_reader (
147+ & self ,
148+ class_hash : ClassHash ,
149+ class_reader : & ClassReader ,
150+ ) -> StateResult < CompiledClasses > {
151+ let contract_class = class_reader. read_executable ( class_hash) ?;
152+ match contract_class {
153+ ContractClass :: V0 ( starknet_api_contract_class) => {
154+ Ok ( CompiledClasses :: V0 ( CompiledClassV0 :: try_from ( starknet_api_contract_class) ?) )
155+ }
156+ ContractClass :: V1 ( ( casm_contract_class, sierra_version) ) => {
157+ if !self . is_declared ( class_hash) ? {
158+ return Err ( StateError :: UndeclaredClassHash ( class_hash) ) ;
159+ }
160+ let sierra = class_reader. read_sierra ( class_hash) ?;
161+ let extracted_sierra_version =
162+ SierraVersion :: extract_from_program ( & sierra. sierra_program ) ?;
163+ if extracted_sierra_version != sierra_version {
164+ let message = format ! (
165+ "Sierra version mismatch. Expected: {sierra_version}, Actual: \
166+ {extracted_sierra_version}"
167+ ) ;
168+ let error = StateError :: CasmAndSierraMismatch { class_hash, message } ;
169+ error ! ( "Error in getting compiled classes: {error:?}" ) ;
170+ return Err ( error) ;
171+ }
172+ Ok ( CompiledClasses :: V1 (
173+ CompiledClassV1 :: try_from ( ( casm_contract_class, sierra_version) ) ?,
174+ Arc :: new ( sierra) ,
175+ ) )
176+ }
177+ }
178+ }
179+
140180 fn read_casm_and_sierra (
141181 & self ,
142182 class_hash : ClassHash ,
@@ -265,7 +305,12 @@ impl StateReader for ApolloReader {
265305
266306impl FetchCompiledClasses for ApolloReader {
267307 fn get_compiled_classes ( & self , class_hash : ClassHash ) -> StateResult < CompiledClasses > {
268- self . get_compiled_class_from_db ( class_hash)
308+ match & self . class_reader {
309+ Some ( class_reader) => {
310+ self . get_compiled_classes_from_class_reader ( class_hash, class_reader)
311+ }
312+ None => self . get_compiled_classes_from_storage ( class_hash) ,
313+ }
269314 }
270315
271316 fn is_declared ( & self , class_hash : ClassHash ) -> StateResult < bool > {
0 commit comments