Skip to content

Commit

Permalink
feat(papyrus_storage): get versioned casm
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 3, 2024
1 parent 6b48bff commit 4466854
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/papyrus_storage/src/compiled_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ mod casm_test;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use papyrus_proc_macros::latency_histogram;
use starknet_api::block::BlockNumber;
use starknet_api::contract_class::{SierraVersion, VersionedCasm};
use starknet_api::core::ClassHash;

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,8 @@ 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 his sierra version given its Sierra class hash.
fn get_versioned_casm(&self, class_hash: &ClassHash) -> StorageResult<Option<VersionedCasm>>;
/// 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 +89,17 @@ impl<Mode: TransactionKind> CasmStorageReader for StorageTxn<'_, Mode> {
casm_location.map(|location| self.file_handlers.get_casm_unchecked(location)).transpose()
}

fn get_versioned_casm(&self, class_hash: &ClassHash) -> StorageResult<Option<VersionedCasm>> {
// TODO(Aviv): consider add a table for the sierra version.
if let Some(casm) = self.get_casm(class_hash)? {
if let Some(sierra) = self.get_class(class_hash)? {
let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)?;
return Ok(Some((casm, sierra_version)));
}
}
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
12 changes: 11 additions & 1 deletion crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use starknet_api::block::{
GasPricePerToken,
StarknetVersion,
};
use starknet_api::contract_class::EntryPointType;
use starknet_api::contract_class::{EntryPointType, SierraVersion};
use starknet_api::core::{
ClassHash,
CompiledClassHash,
Expand Down Expand Up @@ -745,6 +745,16 @@ impl StorageSerde for String {
}
}

impl StorageSerde for SierraVersion {
fn serialize_into(&self, res: &mut impl std::io::Write) -> Result<(), StorageSerdeError> {
serde_json::to_value(self)?.serialize_into(res)
}

fn deserialize_from(bytes: &mut impl std::io::Read) -> Option<Self> {
serde_json::from_value(serde_json::Value::deserialize_from(bytes)?).ok()
}
}

impl<T: StorageSerde> StorageSerde for Option<T> {
fn serialize_into(&self, res: &mut impl std::io::Write) -> Result<(), StorageSerdeError> {
match self {
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_api/src/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ impl FromStr for SierraVersion {
}
}

/// A tuple of a CasmContractClass and a Sierra Version.
pub type VersionedCasm = (CasmContractClass, SierraVersion);

/// All relevant information about a declared contract class, including the compiled contract class
/// and other parameters derived from the original declare transaction required for billing.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
Expand Down

0 comments on commit 4466854

Please sign in to comment.