Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(starknet_api): rename contract class to sierra contract class #2276

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/global_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cached::{Cached, SizedCache};
use cairo_native::executor::AotContractExecutor;
use starknet_api::core::ClassHash;
#[cfg(feature = "cairo_native")]
use starknet_api::state::ContractClass as SierraContractClass;
use starknet_api::state::SierraContractClass;

#[cfg(feature = "cairo_native")]
use crate::execution::contract_class::RunnableContractClass;
Expand Down
4 changes: 2 additions & 2 deletions crates/native_blockifier/src/py_block_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use pretty_assertions::assert_eq;
use starknet_api::class_hash;
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;
use starknet_types_core::felt::Felt;

use crate::py_block_executor::{PyBlockExecutor, PyOsConfig};
Expand All @@ -31,7 +31,7 @@ fn global_contract_cache_update() {
pythonic_hints: Default::default(),
entry_points_by_type: Default::default(),
};
let sierra = ContractClass::default();
let sierra = SierraContractClass::default();
let contract_class =
RunnableContractClass::V1(ContractClassV1::try_from(casm.clone()).unwrap());
let class_hash = class_hash!("0x1");
Expand Down
7 changes: 4 additions & 3 deletions crates/native_blockifier/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet_api::block::{BlockHash, BlockHeader, BlockHeaderWithoutHash, BlockN
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::hash::StarkHash;
use starknet_api::state::{ContractClass, StateDiff, StateNumber, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StateDiff, StateNumber, ThinStateDiff};

use crate::errors::NativeBlockifierResult;
use crate::py_state_diff::PyBlockInfo;
Expand Down Expand Up @@ -170,7 +170,8 @@ impl Storage for PapyrusStorage {
py_state_diff.address_to_class_hash.remove(&address.into());
});

let mut declared_classes = IndexMap::<ClassHash, (CompiledClassHash, ContractClass)>::new();
let mut declared_classes =
IndexMap::<ClassHash, (CompiledClassHash, SierraContractClass)>::new();
let mut undeclared_casm_contracts = Vec::<(ClassHash, CasmContractClass)>::new();
for (class_hash, (raw_sierra, (compiled_class_hash, raw_casm))) in
declared_class_hash_to_class
Expand All @@ -184,7 +185,7 @@ impl Storage for PapyrusStorage {
.is_none();

if class_undeclared {
let sierra_contract_class: ContractClass = serde_json::from_str(&raw_sierra)?;
let sierra_contract_class: SierraContractClass = serde_json::from_str(&raw_sierra)?;
declared_classes.insert(
class_hash,
(CompiledClassHash(compiled_class_hash.0), sierra_contract_class),
Expand Down
9 changes: 6 additions & 3 deletions crates/papyrus_common/src/class_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sha3::Digest;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::ClassHash;
use starknet_api::hash::PoseidonHash;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Poseidon, StarkHash};

Expand All @@ -18,7 +18,7 @@ lazy_static! {

/// Calculates the hash of a contract class.
// Based on Pathfinder code (the starknet.io doc is incorrect).
pub fn calculate_class_hash(class: &ContractClass) -> ClassHash {
pub fn calculate_class_hash(class: &SierraContractClass) -> ClassHash {
let external_entry_points_hash = entry_points_hash(class, &EntryPointType::External);
let l1_handler_entry_points_hash = entry_points_hash(class, &EntryPointType::L1Handler);
let constructor_entry_points_hash = entry_points_hash(class, &EntryPointType::Constructor);
Expand All @@ -38,7 +38,10 @@ pub fn calculate_class_hash(class: &ContractClass) -> ClassHash {
ClassHash(class_hash)
}

fn entry_points_hash(class: &ContractClass, entry_point_type: &EntryPointType) -> PoseidonHash {
fn entry_points_hash(
class: &SierraContractClass,
entry_point_type: &EntryPointType,
) -> PoseidonHash {
PoseidonHash(Poseidon::hash_array(
class
.entry_points_by_type
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_common/src/class_hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use starknet_api::class_hash;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;
use starknet_api::test_utils::read_json_file;

use crate::class_hash::calculate_class_hash;

#[test]
fn class_hash() {
let class: ContractClass = serde_json::from_value(read_json_file("class.json")).unwrap();
let class: SierraContractClass = serde_json::from_value(read_json_file("class.json")).unwrap();
let expected_class_hash =
class_hash!("0x29927c8af6bccf3f6fda035981e765a7bdbf18a2dc0d630494f8758aa908e2b");
let calculated_class_hash = calculate_class_hash(&class);
Expand Down
6 changes: 3 additions & 3 deletions crates/papyrus_common/src/pending_classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use starknet_api::core::ClassHash;
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;

pub trait PendingClassesTrait {
// TODO(shahak) Return an Arc to avoid cloning the class. This requires to re-implement
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct PendingClasses {
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ApiContractClass {
DeprecatedContractClass(DeprecatedContractClass),
ContractClass(ContractClass),
ContractClass(SierraContractClass),
}

impl ApiContractClass {
Expand All @@ -44,7 +44,7 @@ impl ApiContractClass {
}
}

pub fn into_cairo1(self) -> Option<ContractClass> {
pub fn into_cairo1(self) -> Option<SierraContractClass> {
match self {
Self::ContractClass(class) => Some(class),
_ => None,
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_execution/src/state_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use papyrus_storage::test_utils::get_test_storage;
use starknet_api::block::{BlockBody, BlockHash, BlockHeader, BlockHeaderWithoutHash, BlockNumber};
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce};
use starknet_api::hash::StarkHash;
use starknet_api::state::{ContractClass, StateNumber, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StateNumber, ThinStateDiff};
use starknet_api::{contract_address, felt, storage_key};
use starknet_types_core::felt::Felt;

Expand All @@ -47,7 +47,7 @@ fn read_state() {
let storage_value0 = felt!(777_u128);
let storage_value1 = felt!(888_u128);
// The class is not used in the execution, so it can be default.
let class0 = ContractClass::default();
let class0 = SierraContractClass::default();
let casm0 = get_test_casm();
let blockifier_casm0 =
RunnableContractClass::V1(ContractClassV1::try_from(casm0.clone()).unwrap());
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_execution/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use starknet_api::core::{
SequencerContractAddress,
};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{ContractClass, StateNumber, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StateNumber, ThinStateDiff};
use starknet_api::test_utils::read_json_file;
use starknet_api::transaction::fields::Fee;
use starknet_api::transaction::{
Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn prepare_storage(mut storage_writer: StorageWriter) {
.unwrap()
.append_classes(
BlockNumber(0),
&[(class_hash0, &ContractClass::default())],
&[(class_hash0, &SierraContractClass::default())],
&[
(*TEST_ERC20_CONTRACT_CLASS_HASH, &get_test_erc20_fee_contract_class()),
(class_hash1, &get_test_deprecated_contract_class()),
Expand Down
6 changes: 3 additions & 3 deletions crates/papyrus_p2p_sync/src/server/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use starknet_api::block::{
BlockSignature,
};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;
use starknet_api::transaction::{
Event,
FullTransaction,
Expand Down Expand Up @@ -491,15 +491,15 @@ lazy_static! {
.into_iter()
.flat_map(|tx_output| tx_output.into_iter().flat_map(|output| output.events().to_vec()))
.collect();
static ref CLASSES_WITH_HASHES: Vec<Vec<(ClassHash, ContractClass)>> = {
static ref CLASSES_WITH_HASHES: Vec<Vec<(ClassHash, SierraContractClass)>> = {
THIN_STATE_DIFFS
.iter()
.map(|state_diff| {
let class_vec = state_diff
.declared_classes
.iter()
.map(|(class_hash, _)| {
(*class_hash, ContractClass::get_test_instance(&mut get_rng()))
(*class_hash, SierraContractClass::get_test_instance(&mut get_rng()))
})
.collect::<Vec<_>>();
class_vec
Expand Down
10 changes: 5 additions & 5 deletions crates/papyrus_protobuf/src/converters/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl TryFrom<protobuf::Class> for (ApiContractClass, ClassHash) {
)
}
Some(protobuf::class::Class::Cairo1(class)) => {
ApiContractClass::ContractClass(state::ContractClass::try_from(class)?)
ApiContractClass::ContractClass(state::SierraContractClass::try_from(class)?)
}
None => {
return Err(ProtobufConversionError::MissingField {
Expand Down Expand Up @@ -199,7 +199,7 @@ impl From<deprecated_contract_class::ContractClass> for protobuf::Cairo0Class {
}
}

impl TryFrom<protobuf::Cairo1Class> for state::ContractClass {
impl TryFrom<protobuf::Cairo1Class> for state::SierraContractClass {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::Cairo1Class) -> Result<Self, Self::Error> {
let abi = value.abi;
Expand Down Expand Up @@ -243,12 +243,12 @@ impl TryFrom<protobuf::Cairo1Class> for state::ContractClass {
);
}

Ok(state::ContractClass { sierra_program, entry_points_by_type, abi })
Ok(state::SierraContractClass { sierra_program, entry_points_by_type, abi })
}
}

impl From<state::ContractClass> for protobuf::Cairo1Class {
fn from(value: state::ContractClass) -> Self {
impl From<state::SierraContractClass> for protobuf::Cairo1Class {
fn from(value: state::SierraContractClass) -> Self {
let abi = value.abi;

let program =
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_8/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use starknet_api::deprecated_contract_class::{
FunctionStateMutability,
};
use starknet_api::hash::StarkHash;
use starknet_api::state::{ContractClass as StarknetApiContractClass, StateDiff};
use starknet_api::state::{SierraContractClass as StarknetApiContractClass, StateDiff};
use starknet_api::transaction::{
Event as StarknetApiEvent,
EventContent,
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_rpc/src/v0_8/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ async fn write_block_0_as_pending(
.unwrap();
let class_hash1 = class_hash!("0x1");

let class2 = starknet_api::state::ContractClass::default();
let class2 = starknet_api::state::SierraContractClass::default();
let casm = serde_json::from_value::<CasmContractClass>(read_json_file("casm.json")).unwrap();
let class_hash2 = class_hash!("0x2");
let compiled_class_hash = CompiledClassHash(StarkHash::default());
Expand Down Expand Up @@ -1653,7 +1653,7 @@ fn prepare_storage_for_execution(mut storage_writer: StorageWriter) -> StorageWr
.unwrap();
let class_hash1 = class_hash!("0x1");

let class2 = starknet_api::state::ContractClass::default();
let class2 = starknet_api::state::SierraContractClass::default();
let casm = serde_json::from_value::<CasmContractClass>(read_json_file("casm.json")).unwrap();
let class_hash2 = class_hash!("0x2");
let compiled_class_hash = CompiledClassHash(StarkHash::default());
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_rpc/src/v0_8/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ pub struct ContractClass {
pub abi: String,
}

impl From<starknet_api::state::ContractClass> for ContractClass {
fn from(class: starknet_api::state::ContractClass) -> Self {
impl From<starknet_api::state::SierraContractClass> for ContractClass {
fn from(class: starknet_api::state::SierraContractClass) -> Self {
Self {
sierra_program: class.sierra_program,
contract_class_version: CONTRACT_CLASS_VERSION.to_owned(),
Expand Down
16 changes: 8 additions & 8 deletions crates/papyrus_storage/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! use starknet_api::core::{ClassHash, CompiledClassHash};
//! use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
//! use starknet_api::hash::StarkHash;
//! use starknet_api::state::{ContractClass, ThinStateDiff};
//! use starknet_api::state::{SierraContractClass, ThinStateDiff};
//!
//! # let dir_handle = tempfile::tempdir().unwrap();
//! # let dir = dir_handle.path().to_path_buf();
Expand All @@ -33,7 +33,7 @@
//! # };
//! # let storage_config = StorageConfig{db_config, ..Default::default()};
//! let class_hash = ClassHash::default();
//! let class = ContractClass::default();
//! let class = SierraContractClass::default();
//! let deprecated_class_hash = ClassHash(StarkHash::ONE);
//! let deprecated_class = DeprecatedContractClass::default();
//! let (reader, mut writer) = open_storage(storage_config)?;
Expand Down Expand Up @@ -71,7 +71,7 @@ use papyrus_proc_macros::latency_histogram;
use starknet_api::block::BlockNumber;
use starknet_api::core::ClassHash;
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::ContractClass;
use starknet_api::state::SierraContractClass;

use crate::db::table_types::Table;
use crate::db::{TransactionKind, RW};
Expand All @@ -90,7 +90,7 @@ use crate::{
/// Interface for reading data related to classes or deprecated classes.
pub trait ClassStorageReader {
/// Returns the Cairo 1 class with the given hash.
fn get_class(&self, class_hash: &ClassHash) -> StorageResult<Option<ContractClass>>;
fn get_class(&self, class_hash: &ClassHash) -> StorageResult<Option<SierraContractClass>>;

/// Returns the Cairo 0 class with the given hash.
fn get_deprecated_class(
Expand Down Expand Up @@ -119,13 +119,13 @@ where
fn append_classes(
self,
block_number: BlockNumber,
classes: &[(ClassHash, &ContractClass)],
classes: &[(ClassHash, &SierraContractClass)],
deprecated_classes: &[(ClassHash, &DeprecatedContractClass)],
) -> StorageResult<Self>;
}

impl<'env, Mode: TransactionKind> ClassStorageReader for StorageTxn<'env, Mode> {
fn get_class(&self, class_hash: &ClassHash) -> StorageResult<Option<ContractClass>> {
fn get_class(&self, class_hash: &ClassHash) -> StorageResult<Option<SierraContractClass>> {
let declared_classes_table = self.open_table(&self.tables.declared_classes)?;
let contract_class_location = declared_classes_table.get(&self.txn, class_hash)?;
contract_class_location
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'env> ClassStorageWriter for StorageTxn<'env, RW> {
fn append_classes(
self,
block_number: BlockNumber,
classes: &[(ClassHash, &ContractClass)],
classes: &[(ClassHash, &SierraContractClass)],
deprecated_classes: &[(ClassHash, &DeprecatedContractClass)],
) -> StorageResult<Self> {
let declared_classes_table = self.open_table(&self.tables.declared_classes)?;
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'env> ClassStorageWriter for StorageTxn<'env, RW> {
}

fn write_classes<'env>(
classes: &[(ClassHash, &ContractClass)],
classes: &[(ClassHash, &SierraContractClass)],
txn: &DbTransaction<'env, RW>,
declared_classes_table: &'env DeclaredClassesTable<'env>,
file_handlers: &FileHandlers<RW>,
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_storage/src/class_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet_api::block::BlockNumber;
use starknet_api::core::{ClassHash, CompiledClassHash};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::hash::StarkHash;
use starknet_api::state::{ContractClass, StateNumber, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StateNumber, ThinStateDiff};
use starknet_api::test_utils::read_json_file;

use super::{ClassStorageReader, ClassStorageWriter};
Expand All @@ -16,7 +16,7 @@ use crate::StorageError;
#[test]
fn append_classes_writes_correct_data() {
let class_json = read_json_file("class.json");
let expected_class: ContractClass = serde_json::from_value(class_json).unwrap();
let expected_class: SierraContractClass = serde_json::from_value(class_json).unwrap();
let deprecated_class_json = read_json_file("deprecated_class.json");
let expected_deprecated_class: DeprecatedContractClass =
serde_json::from_value(deprecated_class_json).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockHash, BlockNumber, BlockSignature, StarknetVersion};
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{ContractClass, StateNumber, StorageKey, ThinStateDiff};
use starknet_api::state::{SierraContractClass, StateNumber, StorageKey, ThinStateDiff};
use starknet_api::transaction::{Transaction, TransactionHash, TransactionOutput};
use starknet_types_core::felt::Felt;
use tracing::{debug, warn};
Expand Down Expand Up @@ -666,7 +666,7 @@ pub(crate) type MarkersTable<'env> =
#[derive(Clone, Debug)]
struct FileHandlers<Mode: TransactionKind> {
thin_state_diff: FileHandler<VersionZeroWrapper<ThinStateDiff>, Mode>,
contract_class: FileHandler<VersionZeroWrapper<ContractClass>, Mode>,
contract_class: FileHandler<VersionZeroWrapper<SierraContractClass>, Mode>,
casm: FileHandler<VersionZeroWrapper<CasmContractClass>, Mode>,
deprecated_contract_class: FileHandler<VersionZeroWrapper<DeprecatedContractClass>, Mode>,
transaction_output: FileHandler<VersionZeroWrapper<TransactionOutput>, Mode>,
Expand All @@ -681,7 +681,7 @@ impl FileHandlers<RW> {
}

// Appends a contract class to the corresponding file and returns its location.
fn append_contract_class(&self, contract_class: &ContractClass) -> LocationInFile {
fn append_contract_class(&self, contract_class: &SierraContractClass) -> LocationInFile {
self.clone().contract_class.append(contract_class)
}

Expand Down Expand Up @@ -748,7 +748,7 @@ impl<Mode: TransactionKind> FileHandlers<Mode> {
fn get_contract_class_unchecked(
&self,
location: LocationInFile,
) -> StorageResult<ContractClass> {
) -> StorageResult<SierraContractClass> {
self.contract_class.get(location)?.ok_or(StorageError::DBInconsistency {
msg: format!("ContractClass at location {:?} not found.", location),
})
Expand Down
Loading
Loading