Skip to content

Commit

Permalink
refactor(katana-primitives): move class related types to a separate m…
Browse files Browse the repository at this point in the history
…odule (#1560)
  • Loading branch information
kariy committed Mar 7, 2024
1 parent e4e982a commit d71c246
Show file tree
Hide file tree
Showing 40 changed files with 146 additions and 154 deletions.
3 changes: 2 additions & 1 deletion bin/katana/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};
use console::Style;
use katana_core::sequencer::KatanaSequencer;
use katana_primitives::contract::{ClassHash, ContractAddress};
use katana_primitives::class::ClassHash;
use katana_primitives::contract::ContractAddress;
use katana_primitives::genesis::allocation::GenesisAccountAlloc;
use katana_primitives::genesis::Genesis;
use katana_rpc::{spawn, NodeHandle};
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/core/src/backend/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use katana_primitives::contract::DeprecatedCompiledClass;
use katana_primitives::class::DeprecatedCompiledClass;
use starknet::core::types::FlattenedSierraClass;

pub enum StarknetContract {
Expand Down
5 changes: 2 additions & 3 deletions crates/katana/core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use katana_executor::blockifier::utils::{block_context_from_envs, EntryPointCall
use katana_executor::blockifier::PendingState;
use katana_primitives::block::{BlockHash, BlockHashOrNumber, BlockIdOrTag, BlockNumber};
use katana_primitives::chain::ChainId;
use katana_primitives::contract::{
ClassHash, CompiledClass, ContractAddress, Nonce, StorageKey, StorageValue,
};
use katana_primitives::class::{ClassHash, CompiledClass};
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
use katana_primitives::event::{ContinuationToken, ContinuationTokenError};
use katana_primitives::receipt::Event;
use katana_primitives::transaction::{ExecutableTxWithHash, TxHash, TxWithHash};
Expand Down
32 changes: 14 additions & 18 deletions crates/katana/executor/src/blockifier/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use blockifier::state::cached_state::{CachedState, GlobalContractCache};
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use katana_primitives::contract::{CompiledClass, FlattenedSierraClass};
use katana_primitives::class::{CompiledClass, FlattenedSierraClass};
use katana_primitives::conversion::blockifier::to_class;
use katana_primitives::FieldElement;
use katana_provider::traits::contract::ContractClassProvider;
Expand All @@ -16,9 +16,8 @@ use starknet_api::patricia_key;
use starknet_api::state::StorageKey;

mod primitives {
pub use katana_primitives::contract::{
ClassHash, CompiledClassHash, ContractAddress, Nonce, StorageKey, StorageValue,
};
pub use katana_primitives::class::{ClassHash, CompiledClassHash};
pub use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
}

/// A state db only provide read access.
Expand Down Expand Up @@ -167,10 +166,7 @@ impl CachedStateWrapper {
}

impl ContractClassProvider for CachedStateWrapper {
fn class(
&self,
hash: katana_primitives::contract::ClassHash,
) -> ProviderResult<Option<CompiledClass>> {
fn class(&self, hash: primitives::ClassHash) -> ProviderResult<Option<CompiledClass>> {
if let res @ Some(_) = self.class_cache.read().compiled.get(&hash).cloned() {
Ok(res)
} else {
Expand All @@ -180,8 +176,8 @@ impl ContractClassProvider for CachedStateWrapper {

fn compiled_class_hash_of_class_hash(
&self,
hash: katana_primitives::contract::ClassHash,
) -> ProviderResult<Option<katana_primitives::contract::CompiledClassHash>> {
hash: primitives::ClassHash,
) -> ProviderResult<Option<primitives::CompiledClassHash>> {
let Ok(hash) = self.inner().get_compiled_class_hash(ClassHash(hash.into())) else {
return Ok(None);
};
Expand All @@ -190,7 +186,7 @@ impl ContractClassProvider for CachedStateWrapper {

fn sierra_class(
&self,
hash: katana_primitives::contract::ClassHash,
hash: primitives::ClassHash,
) -> ProviderResult<Option<FlattenedSierraClass>> {
if let Some(class) = self.class_cache.read().sierra.get(&hash) {
Ok(Some(class.clone()))
Expand All @@ -203,9 +199,9 @@ impl ContractClassProvider for CachedStateWrapper {
impl StateProvider for CachedStateWrapper {
fn storage(
&self,
address: katana_primitives::contract::ContractAddress,
storage_key: katana_primitives::contract::StorageKey,
) -> ProviderResult<Option<katana_primitives::contract::StorageValue>> {
address: primitives::ContractAddress,
storage_key: primitives::StorageKey,
) -> ProviderResult<Option<primitives::StorageValue>> {
let Ok(value) =
self.inner().get_storage_at(address.into(), StorageKey(patricia_key!(storage_key)))
else {
Expand All @@ -216,8 +212,8 @@ impl StateProvider for CachedStateWrapper {

fn nonce(
&self,
address: katana_primitives::contract::ContractAddress,
) -> ProviderResult<Option<katana_primitives::contract::Nonce>> {
address: primitives::ContractAddress,
) -> ProviderResult<Option<primitives::Nonce>> {
let Ok(nonce) = self.inner().get_nonce_at(address.into()) else {
return Ok(None);
};
Expand All @@ -226,8 +222,8 @@ impl StateProvider for CachedStateWrapper {

fn class_hash_of_contract(
&self,
address: katana_primitives::contract::ContractAddress,
) -> ProviderResult<Option<katana_primitives::contract::ClassHash>> {
address: primitives::ContractAddress,
) -> ProviderResult<Option<primitives::ClassHash>> {
let Ok(hash) = self.inner().get_class_hash_at(address.into()) else {
return Ok(None);
};
Expand Down
40 changes: 21 additions & 19 deletions crates/katana/executor/src/blockifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ pub fn get_state_update_from_cached_state(
(class_hash.0.into(), class)
})
.collect::<HashMap<
katana_primitives::contract::ClassHash,
katana_primitives::contract::CompiledClass,
katana_primitives::class::ClassHash,
katana_primitives::class::CompiledClass,
>>();

let nonce_updates =
Expand Down Expand Up @@ -281,23 +281,25 @@ pub fn get_state_update_from_cached_state(
})
.collect::<HashMap<katana_primitives::contract::ContractAddress, _>>();

let contract_updates = state_diff
.address_to_class_hash
.into_iter()
.map(|(key, value)| (key.into(), value.0.into()))
.collect::<HashMap<
katana_primitives::contract::ContractAddress,
katana_primitives::contract::ClassHash,
>>();

let declared_classes = state_diff
.class_hash_to_compiled_class_hash
.into_iter()
.map(|(key, value)| (key.0.into(), value.0.into()))
.collect::<HashMap<
katana_primitives::contract::ClassHash,
katana_primitives::contract::CompiledClassHash,
>>();
let contract_updates =
state_diff
.address_to_class_hash
.into_iter()
.map(|(key, value)| (key.into(), value.0.into()))
.collect::<HashMap<
katana_primitives::contract::ContractAddress,
katana_primitives::class::ClassHash,
>>();

let declared_classes =
state_diff
.class_hash_to_compiled_class_hash
.into_iter()
.map(|(key, value)| (key.0.into(), value.0.into()))
.collect::<HashMap<
katana_primitives::class::ClassHash,
katana_primitives::class::CompiledClassHash,
>>();

StateUpdatesWithDeclaredClasses {
declared_sierra_classes,
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ strum_macros.workspace = true
thiserror.workspace = true

blockifier.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-starknet.workspace = true
flate2.workspace = true
starknet_api.workspace = true

Expand Down
38 changes: 38 additions & 0 deletions crates/katana/primitives/src/class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use cairo_lang_starknet::casm_contract_class::CasmContractClass;

use crate::FieldElement;

/// The canonical hash of a contract class. This is the identifier of a class.
pub type ClassHash = FieldElement;
/// The hash of a compiled contract class.
pub type CompiledClassHash = FieldElement;

pub type SierraClass = starknet::core::types::contract::SierraClass;
pub type FlattenedSierraClass = starknet::core::types::FlattenedSierraClass;

/// Deprecated legacy (Cairo 0) CASM class
pub type DeprecatedCompiledClass = ::starknet_api::deprecated_contract_class::ContractClass;

/// Represents an executable Sierra program.
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SierraProgram {
pub program: cairo_lang_sierra::program::Program,
pub entry_points_by_type: cairo_lang_starknet::contract_class::ContractEntryPoints,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SierraCompiledClass {
pub casm: CasmContractClass,
pub sierra: SierraProgram,
}

/// Executable contract class
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CompiledClass {
Class(SierraCompiledClass),
Deprecated(DeprecatedCompiledClass),
}
35 changes: 1 addition & 34 deletions crates/katana/primitives/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
use std::fmt;

use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use derive_more::Deref;
use starknet::core::utils::normalize_address;

use crate::class::ClassHash;
use crate::FieldElement;

/// Represents the type for a contract storage key.
pub type StorageKey = FieldElement;
/// Represents the type for a contract storage value.
pub type StorageValue = FieldElement;

/// The canonical hash of a contract class. This is the class hash value of a contract instance.
pub type ClassHash = FieldElement;
/// The hash of a compiled contract class.
pub type CompiledClassHash = FieldElement;

/// Represents the type for a contract nonce.
pub type Nonce = FieldElement;

pub type SierraClass = starknet::core::types::contract::SierraClass;
pub type FlattenedSierraClass = starknet::core::types::FlattenedSierraClass;

/// Represents a contract address.
#[derive(Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Debug, Deref)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -60,28 +52,3 @@ pub struct GenericContractInfo {
/// The hash of the contract class.
pub class_hash: ClassHash,
}

pub type DeprecatedCompiledClass = ::starknet_api::deprecated_contract_class::ContractClass;

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SierraProgram {
pub program: cairo_lang_sierra::program::Program,
pub entry_points_by_type: cairo_lang_starknet::contract_class::ContractEntryPoints,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SierraCompiledClass {
pub casm: CasmContractClass,
pub sierra: SierraProgram,
}

/// Executable contract class
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CompiledClass {
Deprecated(DeprecatedCompiledClass),
Class(SierraCompiledClass),
}
2 changes: 1 addition & 1 deletion crates/katana/primitives/src/conversion/blockifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_api::hash::StarkHash;
use starknet_api::patricia_key;

use crate::chain::ChainId;
use crate::contract::CompiledClass;
use crate::class::CompiledClass;

impl From<crate::contract::ContractAddress> for ContractAddress {
fn from(address: crate::contract::ContractAddress) -> Self {
Expand Down
18 changes: 9 additions & 9 deletions crates/katana/primitives/src/conversion/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use starknet_api::deprecated_contract_class::{
ContractClassAbiEntry, EntryPoint, EntryPointType, TypedParameter,
};

use crate::contract::{
use crate::class::{
ClassHash, CompiledClassHash, DeprecatedCompiledClass, FlattenedSierraClass,
SierraCompiledClass, SierraProgram,
};
Expand Down Expand Up @@ -135,11 +135,11 @@ pub fn legacy_inner_to_rpc_class(
Ok(ContractClass::Legacy(CompressedLegacyContractClass { abi, program, entry_points_by_type }))
}

/// Convert the given [`FlattenedSierraClass`] into the inner compiled class type
/// [`CompiledClassHash`] along with its class hashes.
/// Convert the given [FlattenedSierraClass] into the inner compiled class type
/// [CompiledClass](crate::class::CompiledClass) along with its class hashes.
pub fn flattened_sierra_to_compiled_class(
contract_class: &FlattenedSierraClass,
) -> Result<(ClassHash, CompiledClassHash, crate::contract::CompiledClass)> {
) -> Result<(ClassHash, CompiledClassHash, crate::class::CompiledClass)> {
let class_hash = contract_class.class_hash();

let class = rpc_to_cairo_contract_class(contract_class)?;
Expand All @@ -151,7 +151,7 @@ pub fn flattened_sierra_to_compiled_class(
let casm = CasmContractClass::from_contract_class(class, true)?;
let compiled_hash = FieldElement::from_bytes_be(&casm.compiled_class_hash().to_be_bytes())?;

let class = crate::contract::CompiledClass::Class(SierraCompiledClass { casm, sierra });
let class = crate::class::CompiledClass::Class(SierraCompiledClass { casm, sierra });
Ok((class_hash, compiled_hash, class))
}

Expand All @@ -166,10 +166,10 @@ pub fn compiled_class_hash_from_flattened_sierra_class(
}

/// Converts a legacy RPC compiled contract class [CompressedLegacyContractClass] type to the inner
/// compiled class type [CompiledClass](crate::contract::CompiledClass) along with its class hash.
/// compiled class type [CompiledClass](crate::class::CompiledClass) along with its class hash.
pub fn legacy_rpc_to_compiled_class(
compressed_legacy_contract: &CompressedLegacyContractClass,
) -> Result<(ClassHash, crate::contract::CompiledClass)> {
) -> Result<(ClassHash, crate::class::CompiledClass)> {
let class_json = json!({
"abi": compressed_legacy_contract.abi.clone().unwrap_or_default(),
"entry_points_by_type": compressed_legacy_contract.entry_points_by_type,
Expand All @@ -179,7 +179,7 @@ pub fn legacy_rpc_to_compiled_class(
let deprecated_class: DeprecatedCompiledClass = serde_json::from_value(class_json.clone())?;
let class_hash = serde_json::from_value::<LegacyContractClass>(class_json)?.class_hash()?;

Ok((class_hash, crate::contract::CompiledClass::Deprecated(deprecated_class)))
Ok((class_hash, crate::class::CompiledClass::Deprecated(deprecated_class)))
}

/// Converts `starknet-rs` RPC [FlattenedSierraClass] type to Cairo's
Expand Down Expand Up @@ -274,7 +274,7 @@ mod tests {
use starknet::core::types::ContractClass;

use super::{legacy_inner_to_rpc_class, legacy_rpc_to_compiled_class};
use crate::contract::{CompiledClass, DeprecatedCompiledClass};
use crate::class::{CompiledClass, DeprecatedCompiledClass};
use crate::genesis::constant::DEFAULT_OZ_ACCOUNT_CONTRACT;
use crate::utils::class::parse_deprecated_compiled_class;

Expand Down
3 changes: 2 additions & 1 deletion crates/katana/primitives/src/genesis/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use starknet::core::utils::get_contract_address;
use starknet::signers::SigningKey;

use super::constant::DEFAULT_OZ_ACCOUNT_CONTRACT_CLASS_HASH;
use crate::contract::{ClassHash, ContractAddress, StorageKey, StorageValue};
use crate::class::ClassHash;
use crate::contract::{ContractAddress, StorageKey, StorageValue};
use crate::FieldElement;

/// Represents a contract allocation in the genesis block.
Expand Down
5 changes: 2 additions & 3 deletions crates/katana/primitives/src/genesis/constant.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use lazy_static::lazy_static;
use starknet::core::utils::get_storage_var_address;

use crate::contract::{
ClassHash, CompiledClass, CompiledClassHash, ContractAddress, SierraClass, StorageKey,
};
use crate::class::{ClassHash, CompiledClass, CompiledClassHash, SierraClass};
use crate::contract::{ContractAddress, StorageKey};
use crate::utils::class::{parse_compiled_class, parse_sierra_class};
use crate::FieldElement;

Expand Down
5 changes: 2 additions & 3 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ use super::constant::{
};
use super::{FeeTokenConfig, Genesis, GenesisAllocation, UniversalDeployerConfig};
use crate::block::{BlockHash, BlockNumber, GasPrices};
use crate::contract::{
ClassHash, CompiledClass, ContractAddress, SierraClass, StorageKey, StorageValue,
};
use crate::class::{ClassHash, CompiledClass, SierraClass};
use crate::contract::{ContractAddress, StorageKey, StorageValue};
use crate::genesis::GenesisClass;
use crate::utils::class::{parse_compiled_class_v1, parse_deprecated_compiled_class};
use crate::FieldElement;
Expand Down
6 changes: 2 additions & 4 deletions crates/katana/primitives/src/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ use self::constant::{
OZ_ACCOUNT_CONTRACT_PUBKEY_STORAGE_SLOT,
};
use crate::block::{Block, BlockHash, BlockNumber, GasPrices, Header};
use crate::contract::{
ClassHash, CompiledClass, CompiledClassHash, ContractAddress, FlattenedSierraClass, StorageKey,
StorageValue,
};
use crate::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use crate::contract::{ContractAddress, StorageKey, StorageValue};
use crate::state::StateUpdatesWithDeclaredClasses;
use crate::utils::split_u256;
use crate::version::CURRENT_STARKNET_VERSION;
Expand Down
Loading

0 comments on commit d71c246

Please sign in to comment.