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

feat(starknet_sierra_compile_types): add crate #3245

Merged
merged 1 commit into from
Jan 13, 2025
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
14 changes: 14 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 @@ -51,6 +51,7 @@ members = [
"crates/starknet_sequencer_infra",
"crates/starknet_sequencer_node",
"crates/starknet_sierra_compile",
"crates/starknet_sierra_compile_types",
"crates/starknet_state_sync",
"crates/starknet_state_sync_types",
"crates/starknet_task_executor",
Expand Down Expand Up @@ -239,6 +240,7 @@ starknet_patricia = { path = "crates/starknet_patricia", version = "0.0.0" }
starknet_sequencer_infra = { path = "crates/starknet_sequencer_infra", version = "0.0.0" }
starknet_sequencer_node = { path = "crates/starknet_sequencer_node", version = "0.0.0" }
starknet_sierra_compile = { path = "crates/starknet_sierra_compile", version = "0.0.0" }
starknet_sierra_compile_types = { path = "crates/starknet_sierra_compile_types", version = "0.0.0" }
starknet_state_sync = { path = "crates/starknet_state_sync", version = "0.0.0" }
starknet_state_sync_types = { path = "crates/starknet_state_sync_types", version = "0.0.0" }
starknet_task_executor = { path = "crates/starknet_task_executor", 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 @@ -72,6 +72,7 @@ const Configuration = {
'starknet_sequencer_infra',
'starknet_sequencer_node',
'starknet_sierra_compile',
'starknet_sierra_compile_types',
'starknet_state_sync',
'starknet_state_sync_types',
'starknet_task_executor',
Expand Down
19 changes: 19 additions & 0 deletions crates/starknet_sierra_compile_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "starknet_sierra_compile_types"
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[lints]
workspace = true

[dependencies]
async-trait.workspace = true
bytes = { workspace = true, features = ["serde"] }
papyrus_proc_macros.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
starknet_api.workspace = true
starknet_sequencer_infra.workspace = true
thiserror.workspace = true
120 changes: 120 additions & 0 deletions crates/starknet_sierra_compile_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::sync::Arc;

use async_trait::async_trait;
use bytes::Bytes;
use papyrus_proc_macros::handle_response_variants;
use serde::{Deserialize, Serialize};
use starknet_api::contract_class::ContractClass;
use starknet_api::core::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 SierraCompilerResult<T> = Result<T, SierraCompilerError>;
pub type SierraCompilerClientResult<T> = Result<T, SierraCompilerClientError>;

pub type RawExecutableHashedClass = (RawExecutableClass, CompiledClassHash);

pub type SharedSierraCompilerClient = Arc<dyn SierraCompilerClient>;

// TODO(Elin): change to a more efficient serde (bytes, or something similar).
// A prerequisite for this is to solve serde-untagged lack of support.

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RawClass(Bytes);

impl TryFrom<SierraContractClass> for RawClass {
type Error = serde_json::Error;

fn try_from(class: SierraContractClass) -> Result<Self, Self::Error> {
let class = serde_json::to_vec(&class)?.into();
Ok(Self(class))
}
}

impl TryFrom<RawClass> for SierraContractClass {
type Error = serde_json::Error;

fn try_from(class: RawClass) -> Result<Self, Self::Error> {
let class: SierraContractClass = serde_json::from_slice(class.0.as_ref())?;
Ok(class)
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RawExecutableClass(Bytes);

impl TryFrom<ContractClass> for RawExecutableClass {
type Error = serde_json::Error;

fn try_from(class: ContractClass) -> Result<Self, Self::Error> {
let class = serde_json::to_vec(&class)?.into();
Ok(Self(class))
}
}

impl TryFrom<RawExecutableClass> for ContractClass {
type Error = serde_json::Error;

fn try_from(class: RawExecutableClass) -> Result<Self, Self::Error> {
let class: ContractClass = serde_json::from_slice(class.0.as_ref())?;
Ok(class)
}
}

/// Serves as the Sierra compilation unit's shared interface.
/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across
/// threads.
#[async_trait]
pub trait SierraCompilerClient: Send + Sync {
async fn compile(
&self,
class: RawClass,
) -> SierraCompilerClientResult<RawExecutableHashedClass>;
}

#[derive(Clone, Debug, Error, Eq, PartialEq, Serialize, Deserialize)]
pub enum SierraCompilerError {
#[error("Compilation failed: {0}")]
SierraCompilerError(String),
}

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

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SierraCompilerRequest {
Compile(RawClass),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SierraCompilerResponse {
Compile(SierraCompilerResult<RawExecutableHashedClass>),
}

#[async_trait]
impl<ComponentClientType> SierraCompilerClient for ComponentClientType
where
ComponentClientType:
Send + Sync + ComponentClient<SierraCompilerRequest, SierraCompilerResponse>,
{
async fn compile(
&self,
class: RawClass,
) -> SierraCompilerClientResult<RawExecutableHashedClass> {
let request = SierraCompilerRequest::Compile(class);
let response = self.send(request).await;
handle_response_variants!(
SierraCompilerResponse,
Compile,
SierraCompilerClientError,
SierraCompilerError
)
}
}
Loading