Skip to content

Commit

Permalink
feat(starknet_contract_manager_types): add crate
Browse files Browse the repository at this point in the history
  • Loading branch information
elintul committed Dec 17, 2024
1 parent d7be314 commit fa2cb8d
Show file tree
Hide file tree
Showing 6 changed files with 156 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 @@ -35,6 +35,7 @@ members = [
"crates/starknet_client",
"crates/starknet_committer",
"crates/starknet_consensus_manager",
"crates/starknet_contract_manager_types",
"crates/starknet_gateway",
"crates/starknet_gateway_types",
"crates/starknet_http_server",
Expand Down Expand Up @@ -220,6 +221,7 @@ starknet_batcher_types = { path = "crates/starknet_batcher_types", version = "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" }
starknet_contract_manager_types = { path = "crates/starknet_contract_manager_types", version = "0.0.0" }
starknet_gateway = { path = "crates/starknet_gateway", version = "0.0.0" }
starknet_gateway_types = { path = "crates/starknet_gateway_types", version = "0.0.0" }
starknet_http_server = { path = "crates/starknet_http_server", 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_contract_manager_types',
'starknet_gateway',
'starknet_gateway_types',
'starknet_http_server',
Expand Down
18 changes: 18 additions & 0 deletions crates/starknet_contract_manager_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "starknet_contract_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
120 changes: 120 additions & 0 deletions crates/starknet_contract_manager_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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 ContractManagerResult<T> = Result<T, ContractManagerError>;
pub type ContractManagerClientResult<T> = Result<T, ContractManagerClientError>;

// TODO: export.
type ContractId = ClassHash;
type ExecutableClass = CasmContractClass;
type ExecutableClassHash = CompiledClassHash;

/// Serves as the contract manager's shared interface.
/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across
/// threads.
#[async_trait]
pub trait ContractManagerClient: Send + Sync {
// TODO(native): make generic in executable type.
async fn add_contract(
&self,
contract_id: ContractId,
contract: SierraContractClass,
) -> ContractManagerClientResult<ExecutableClassHash>;

async fn get_executable(
&self,
contract_id: ContractId,
) -> ContractManagerClientResult<ExecutableClass>;

async fn get_sierra(
&self,
contract_id: ContractId,
) -> ContractManagerClientResult<SierraContractClass>;
}

#[derive(Clone, Debug, Error, Eq, PartialEq, Serialize, Deserialize)]
pub enum ContractManagerError {
#[error("Compilation failed: {0}")]
CompilationUtilError(String),
#[error("Contract of hash: {contract_id} not found")]
ContractNotFound { contract_id: ContractId },
}

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

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ContractManagerRequest {
AddContract(ContractId, SierraContractClass),
GetExecutable(ContractId),
GetSierra(ContractId),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ContractManagerResponse {
AddContract(ContractManagerResult<ExecutableClassHash>),
GetExecutable(ContractManagerResult<ExecutableClass>),
GetSierra(ContractManagerResult<SierraContractClass>),
}

#[async_trait]
impl<ComponentClientType> ContractManagerClient for ComponentClientType
where
ComponentClientType:
Send + Sync + ComponentClient<ContractManagerRequest, ContractManagerResponse>,
{
async fn add_contract(
&self,
contract_id: ContractId,
contract: SierraContractClass,
) -> ContractManagerClientResult<ExecutableClassHash> {
let request = ContractManagerRequest::AddContract(contract_id, contract);
let response = self.send(request).await;
handle_response_variants!(
ContractManagerResponse,
AddContract,
ContractManagerClientError,
ContractManagerError
)
}

async fn get_executable(
&self,
contract_id: ContractId,
) -> ContractManagerClientResult<ExecutableClass> {
let request = ContractManagerRequest::GetExecutable(contract_id);
let response = self.send(request).await;
handle_response_variants!(
ContractManagerResponse,
GetExecutable,
ContractManagerClientError,
ContractManagerError
)
}

async fn get_sierra(
&self,
contract_id: ContractId,
) -> ContractManagerClientResult<SierraContractClass> {
let request = ContractManagerRequest::GetSierra(contract_id);
let response = self.send(request).await;
handle_response_variants!(
ContractManagerResponse,
GetSierra,
ContractManagerClientError,
ContractManagerError
)
}
}
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 fa2cb8d

Please sign in to comment.