Skip to content

Commit 7318fba

Browse files
committed
apollo_state_reader: get_compiled_classes from class reader
1 parent 0a46d76 commit 7318fba

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_state_reader/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cairo-lang-starknet-classes.workspace = true
2020
starknet-types-core.workspace = true
2121
starknet_api.workspace = true
2222
tokio.workspace = true
23+
tracing.workspace = true
2324

2425
[dev-dependencies]
2526
apollo_storage = { workspace = true, features = ["testing"] }

crates/apollo_state_reader/src/apollo_state.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
2222
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedClass;
2323
use starknet_api::state::{SierraContractClass, StateNumber, StorageKey};
2424
use 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

266306
impl 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> {

crates/starknet_api/src/contract_class/structs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ impl ContractClass {
6363
#[derive(Deref, Serialize, Deserialize, Clone, Debug, Eq, PartialEq, PartialOrd)]
6464
pub struct SierraVersion(Version);
6565

66+
impl Display for SierraVersion {
67+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68+
write!(f, "{}", self.0)
69+
}
70+
}
71+
6672
impl From<SierraVersion> for VersionId {
6773
fn from(val: SierraVersion) -> Self {
6874
VersionId {

0 commit comments

Comments
 (0)