Skip to content

Commit

Permalink
feat(papyrus_storage): get casm and sierra
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 4, 2024
1 parent cb749b9 commit 6475cf1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 3 additions & 1 deletion crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use itertools::Itertools;
use semver::Version;
use serde::de::Error as DeserializationError;
use serde::{Deserialize, Deserializer, Serialize};
use starknet_api::contract_class::{ContractClass, EntryPointType};
use starknet_api::contract_class::{ContractClass, EntryPointType, SierraVersion};
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
Expand Down Expand Up @@ -68,6 +68,8 @@ pub enum RunnableCompiledClass {
V1Native(NativeCompiledClassV1),
}

pub type VersionedRunnableCompiledClass = (RunnableCompiledClass, SierraVersion);

impl TryFrom<ContractClass> for RunnableCompiledClass {
type Error = ProgramError;

Expand Down
23 changes: 15 additions & 8 deletions crates/papyrus_state_reader/src/papyrus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use blockifier::execution::contract_class::{
CompiledClassV0,
CompiledClassV1,
RunnableCompiledClass,
VersionedRunnableCompiledClass,
};
use blockifier::state::errors::StateError;
use blockifier::state::global_cache::GlobalContractCache;
Expand All @@ -11,6 +12,7 @@ use papyrus_storage::db::RO;
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::StorageReader;
use starknet_api::block::BlockNumber;
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::{StateNumber, StorageKey};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -46,7 +48,7 @@ impl PapyrusReader {
fn get_compiled_class_inner(
&self,
class_hash: ClassHash,
) -> StateResult<RunnableCompiledClass> {
) -> StateResult<VersionedRunnableCompiledClass> {
let state_number = StateNumber(self.latest_block);
let class_declaration_block_number = self
.reader()?
Expand All @@ -57,16 +59,20 @@ impl PapyrusReader {
Some(block_number) if block_number <= state_number.0);

if class_is_declared {
let casm_compiled_class = self
let (casm_compiled_class, sierra) = self
.reader()?
.get_casm(&class_hash)
.get_casm_and_sierra(&class_hash)
.map_err(|err| StateError::StateReadError(err.to_string()))?
.expect(
"Should be able to fetch a Casm class if its definition exists, database is \
inconsistent.",
);
let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?;

return Ok(RunnableCompiledClass::V1(CompiledClassV1::try_from(casm_compiled_class)?));
return Ok((
RunnableCompiledClass::V1(CompiledClassV1::try_from(casm_compiled_class)?),
sierra_version,
));
}

let v0_compiled_class = self
Expand All @@ -76,9 +82,10 @@ impl PapyrusReader {
.map_err(|err| StateError::StateReadError(err.to_string()))?;

match v0_compiled_class {
Some(starknet_api_contract_class) => {
Ok(CompiledClassV0::try_from(starknet_api_contract_class)?.into())
}
Some(starknet_api_contract_class) => Ok((
CompiledClassV0::try_from(starknet_api_contract_class)?.into(),
SierraVersion::zero(),
)),
None => Err(StateError::UndeclaredClassHash(class_hash)),
}
}
Expand Down Expand Up @@ -131,7 +138,7 @@ impl StateReader for PapyrusReader {
match contract_class {
Some(contract_class) => Ok(contract_class),
None => {
let contract_class_from_db = self.get_compiled_class_inner(class_hash)?;
let (contract_class_from_db, _) = self.get_compiled_class_inner(class_hash)?;
// The class was declared in a previous (finalized) state; update the global cache.
self.global_class_hash_to_class.set(class_hash, contract_class_from_db.clone());
Ok(contract_class_from_db)
Expand Down
18 changes: 18 additions & 0 deletions crates/papyrus_storage/src/compiled_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use papyrus_proc_macros::latency_histogram;
use starknet_api::block::BlockNumber;
use starknet_api::core::ClassHash;
use starknet_api::state::SierraContractClass;

use crate::class::ClassStorageReader;
use crate::db::serialization::VersionZeroWrapper;
use crate::db::table_types::{SimpleTable, Table};
use crate::db::{DbTransaction, TableHandle, TransactionKind, RW};
Expand All @@ -61,6 +63,12 @@ use crate::{FileHandlers, MarkerKind, MarkersTable, OffsetKind, StorageResult, S
pub trait CasmStorageReader {
/// Returns the Cairo assembly of a class given its Sierra class hash.
fn get_casm(&self, class_hash: &ClassHash) -> StorageResult<Option<CasmContractClass>>;
/// Returns the Cairo assembly of a class and its sierra contract class given its Sierra class
/// hash.
fn get_casm_and_sierra(
&self,
class_hash: &ClassHash,
) -> StorageResult<Option<(CasmContractClass, SierraContractClass)>>;
/// The block marker is the first block number that doesn't exist yet.
///
/// Note: If the last blocks don't contain any declared classes, the marker will point at the
Expand All @@ -85,6 +93,16 @@ impl<Mode: TransactionKind> CasmStorageReader for StorageTxn<'_, Mode> {
casm_location.map(|location| self.file_handlers.get_casm_unchecked(location)).transpose()
}

fn get_casm_and_sierra(
&self,
class_hash: &ClassHash,
) -> StorageResult<Option<(CasmContractClass, SierraContractClass)>> {
match (self.get_casm(class_hash)?, self.get_class(class_hash)?) {
(Some(casm), Some(sierra)) => Ok(Some((casm, sierra))),
(_, _) => Ok(None),
}
}

fn get_compiled_class_marker(&self) -> StorageResult<BlockNumber> {
let markers_table = self.open_table(&self.tables.markers)?;
Ok(markers_table.get(&self.txn, &MarkerKind::CompiledClass)?.unwrap_or_default())
Expand Down
3 changes: 3 additions & 0 deletions crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{SierraContractClass, StateNumber, StorageKey, ThinStateDiff};
use starknet_api::transaction::{Transaction, TransactionHash, TransactionOutput};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::Felt;
use tracing::{debug, warn};
use validator::Validate;
Expand Down Expand Up @@ -590,6 +591,8 @@ pub enum StorageError {
#[error("The table {table_name} is unused under the {storage_scope:?} storage scope.")]
ScopeError { table_name: String, storage_scope: StorageScope },
#[error(transparent)]
StarknetApiError(#[from] StarknetApiError),
#[error(transparent)]
IOError(#[from] std::io::Error),
#[error(transparent)]
SerdeError(#[from] serde_json::Error),
Expand Down

0 comments on commit 6475cf1

Please sign in to comment.