-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(starknet_sierra_compile_types): add crate (#3245)
This is the `_types` crate for the compiler component that the class manager will use (a thin functional wrapper will wrap the existing compiler code in `starknet_sierra_compile` crate, thus will be added there. This is the reason the non-`_types` crate already exists.
- Loading branch information
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |