Skip to content

Commit

Permalink
feat(starknet_class_manager_types): add crate (#2713)
Browse files Browse the repository at this point in the history
  • Loading branch information
elintul authored Dec 18, 2024
1 parent 4dff1fd commit dd0473f
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ members = [
"crates/starknet_api",
"crates/starknet_batcher",
"crates/starknet_batcher_types",
"crates/starknet_class_manager_types",
"crates/starknet_client",
"crates/starknet_committer",
"crates/starknet_consensus_manager",
Expand Down Expand Up @@ -217,6 +218,7 @@ starknet-types-core = "0.1.6"
starknet_api = { path = "crates/starknet_api", version = "0.0.0" }
starknet_batcher = { path = "crates/starknet_batcher", version = "0.0.0" }
starknet_batcher_types = { path = "crates/starknet_batcher_types", version = "0.0.0" }
starknet_class_manager_types = { path = "crates/starknet_class_manager_types", version = "0.0.0" }
starknet_client = { path = "crates/starknet_client", version = "0.0.0" }
starknet_committer = { path = "crates/starknet_committer", version = "0.0.0" }
starknet_consensus_manager = { path = "crates/starknet_consensus_manager", version = "0.0.0" }
Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const Configuration = {
'starknet_client',
'starknet_committer',
'starknet_consensus_manager',
'starknet_class_manager_types',
'starknet_gateway',
'starknet_gateway_types',
'starknet_http_server',
Expand Down
18 changes: 18 additions & 0 deletions crates/starknet_class_manager_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "starknet_class_manager_types"
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[lints]
workspace = true

[dependencies]
async-trait.workspace = true
cairo-lang-starknet-classes.workspace = true
papyrus_proc_macros.workspace = true
serde = { workspace = true, features = ["derive"] }
starknet_api.workspace = true
starknet_sequencer_infra.workspace = true
thiserror.workspace = true
108 changes: 108 additions & 0 deletions crates/starknet_class_manager_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use async_trait::async_trait;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use papyrus_proc_macros::handle_response_variants;
use serde::{Deserialize, Serialize};
use starknet_api::core::{ClassHash, CompiledClassHash};
use starknet_api::state::SierraContractClass;
use starknet_sequencer_infra::component_client::ClientError;
use starknet_sequencer_infra::component_definitions::ComponentClient;
use thiserror::Error;

pub type ClassManagerResult<T> = Result<T, ClassManagerError>;
pub type ClassManagerClientResult<T> = Result<T, ClassManagerClientError>;

// TODO: export.
pub type ClassId = ClassHash;
pub type Class = SierraContractClass;
pub type ExecutableClass = CasmContractClass;
pub type ExecutableClassHash = CompiledClassHash;

/// Serves as the class manager's shared interface.
/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across
/// threads.
#[async_trait]
pub trait ClassManagerClient: Send + Sync {
// TODO(native): make generic in executable type.
async fn add_class(
&self,
class_id: ClassId,
class: Class,
) -> ClassManagerClientResult<ExecutableClassHash>;

async fn get_executable(&self, class_id: ClassId) -> ClassManagerClientResult<ExecutableClass>;

async fn get_sierra(&self, class_id: ClassId) -> ClassManagerClientResult<Class>;
}

#[derive(Clone, Debug, Error, Eq, PartialEq, Serialize, Deserialize)]
pub enum ClassManagerError {
#[error("Compilation failed: {0}")]
CompilationUtilError(String),
#[error("Class of hash: {class_id} not found")]
ClassNotFound { class_id: ClassId },
}

#[derive(Clone, Debug, Error)]
pub enum ClassManagerClientError {
#[error(transparent)]
ClientError(#[from] ClientError),
#[error(transparent)]
ClassManagerError(#[from] ClassManagerError),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ClassManagerRequest {
AddClass(ClassId, Class),
GetExecutable(ClassId),
GetSierra(ClassId),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ClassManagerResponse {
AddClass(ClassManagerResult<ExecutableClassHash>),
GetExecutable(ClassManagerResult<ExecutableClass>),
GetSierra(ClassManagerResult<Class>),
}

#[async_trait]
impl<ComponentClientType> ClassManagerClient for ComponentClientType
where
ComponentClientType: Send + Sync + ComponentClient<ClassManagerRequest, ClassManagerResponse>,
{
async fn add_class(
&self,
class_id: ClassId,
class: Class,
) -> ClassManagerClientResult<ExecutableClassHash> {
let request = ClassManagerRequest::AddClass(class_id, class);
let response = self.send(request).await;
handle_response_variants!(
ClassManagerResponse,
AddClass,
ClassManagerClientError,
ClassManagerError
)
}

async fn get_executable(&self, class_id: ClassId) -> ClassManagerClientResult<ExecutableClass> {
let request = ClassManagerRequest::GetExecutable(class_id);
let response = self.send(request).await;
handle_response_variants!(
ClassManagerResponse,
GetExecutable,
ClassManagerClientError,
ClassManagerError
)
}

async fn get_sierra(&self, class_id: ClassId) -> ClassManagerClientResult<Class> {
let request = ClassManagerRequest::GetSierra(class_id);
let response = self.send(request).await;
handle_response_variants!(
ClassManagerResponse,
GetSierra,
ClassManagerClientError,
ClassManagerError
)
}
}
3 changes: 2 additions & 1 deletion crates/starknet_sierra_compile/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;
use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError;
#[cfg(feature = "cairo_native")]
use cairo_native;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Error)]
#[derive(Clone, Debug, Error, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompilationUtilError {
#[error("Starknet Sierra compilation error: {0}")]
CompilationError(String),
Expand Down

0 comments on commit dd0473f

Please sign in to comment.