Skip to content

Commit ca1a8fb

Browse files
committed
apollo_state_reader: refactor get_compiled_classes_from_storage so it never uses class reader
1 parent 0d25d7b commit ca1a8fb

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

crates/apollo_state_reader/src/apollo_state.rs

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@ impl ClassReader {
4747
Ok(casm)
4848
}
4949

50-
fn read_casm(&self, class_hash: ClassHash) -> StateResult<CasmContractClass> {
51-
let casm = self.read_executable(class_hash)?;
52-
let ContractClass::V1((casm, _sierra_version)) = casm else {
53-
panic!("Class hash {class_hash} originated from a Cairo 1 contract.");
54-
};
55-
56-
Ok(casm)
57-
}
58-
5950
fn read_sierra(&self, class_hash: ClassHash) -> StateResult<SierraContractClass> {
6051
let sierra = self
6152
.runtime
@@ -66,16 +57,6 @@ impl ClassReader {
6657
Ok(sierra)
6758
}
6859

69-
// TODO(Elin): make `read[_optional_deprecated]_casm` symmetrical and independent of invocation
70-
// order.
71-
fn read_optional_deprecated_casm(
72-
&self,
73-
class_hash: ClassHash,
74-
) -> StateResult<Option<DeprecatedClass>> {
75-
let casm = self.read_executable(class_hash)?;
76-
if let ContractClass::V0(casm) = casm { Ok(Some(casm)) } else { Ok(None) }
77-
}
78-
7960
/// Returns the compiled class hash v2 for the given class hash.
8061
fn read_compiled_class_hash_v2(
8162
&self,
@@ -115,14 +96,16 @@ impl ApolloReader {
11596
.map_err(|error| StateError::StateReadError(error.to_string()))
11697
}
11798

118-
// TODO(Arni): Refactor this function so it is clear we use it only when the class reader is not
119-
// set.
12099
/// Returns a V1 contract with Sierra if V1 contract is found, or a V0 contract without Sierra
121100
/// if a V1 contract is not found, or an `Error` otherwise.
122101
fn get_compiled_classes_from_storage(
123102
&self,
124103
class_hash: ClassHash,
125104
) -> StateResult<CompiledClasses> {
105+
assert!(
106+
self.class_reader.is_none(),
107+
"Should not enter this function if the class reader is set"
108+
);
126109
if self.is_declared(class_hash)? {
127110
// Cairo 1.
128111
let (casm_compiled_class, sierra) = self.read_casm_and_sierra(class_hash)?;
@@ -181,39 +164,36 @@ impl ApolloReader {
181164
&self,
182165
class_hash: ClassHash,
183166
) -> StateResult<(CasmContractClass, SierraContractClass)> {
184-
let Some(class_reader) = &self.class_reader else {
185-
// Class reader is not set. Try to read directly from storage.
186-
let (option_casm, option_sierra) = self
187-
.reader()?
188-
.get_casm_and_sierra(&class_hash)
189-
.map_err(|err| StateError::StateReadError(err.to_string()))?;
190-
let (casm, sierra) = couple_casm_and_sierra(class_hash, option_casm, option_sierra)?
191-
.expect(
192-
"Should be able to fetch a Casm and Sierra class if its definition exists,
167+
assert!(
168+
self.class_reader.is_none(),
169+
"Should not enter this function if the class reader is set"
170+
);
171+
let (option_casm, option_sierra) = self
172+
.reader()?
173+
.get_casm_and_sierra(&class_hash)
174+
.map_err(|err| StateError::StateReadError(err.to_string()))?;
175+
let (casm, sierra) = couple_casm_and_sierra(class_hash, option_casm, option_sierra)?
176+
.expect(
177+
"Should be able to fetch a Casm and Sierra class if its definition exists,
193178
database is inconsistent.",
194-
);
195-
196-
return Ok((casm, sierra));
197-
};
179+
);
198180

199-
// TODO(Elin): consider not reading Sierra if compilation is disabled.
200-
Ok((class_reader.read_casm(class_hash)?, class_reader.read_sierra(class_hash)?))
181+
Ok((casm, sierra))
201182
}
202183

203184
fn read_deprecated_casm(&self, class_hash: ClassHash) -> StateResult<Option<DeprecatedClass>> {
204-
let Some(class_reader) = &self.class_reader else {
205-
// Class reader is not set. Try to read directly from storage.
206-
let state_number = StateNumber(self.latest_block);
207-
let option_casm = self
208-
.reader()?
209-
.get_state_reader()
210-
.and_then(|sr| sr.get_deprecated_class_definition_at(state_number, &class_hash))
211-
.map_err(|err| StateError::StateReadError(err.to_string()))?;
212-
213-
return Ok(option_casm);
214-
};
185+
assert!(
186+
self.class_reader.is_none(),
187+
"Should not enter this function if the class reader is set"
188+
);
189+
let state_number = StateNumber(self.latest_block);
190+
let option_casm = self
191+
.reader()?
192+
.get_state_reader()
193+
.and_then(|sr| sr.get_deprecated_class_definition_at(state_number, &class_hash))
194+
.map_err(|err| StateError::StateReadError(err.to_string()))?;
215195

216-
class_reader.read_optional_deprecated_casm(class_hash)
196+
Ok(option_casm)
217197
}
218198

219199
/// Returns the compiled class hash v2 for the given class hash.

0 commit comments

Comments
 (0)