Skip to content

Commit

Permalink
feat: add batcher_types communication
Browse files Browse the repository at this point in the history
commit-id:aa33dbd8
  • Loading branch information
Itay-Tsabary-Starkware committed Aug 1, 2024
1 parent 84bf732 commit ad45c95
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions crates/batcher_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ license.workspace = true
repository.workspace = true


[dependencies]

[lints]
workspace = true

[dependencies]
async-trait.workspace = true
mockall.workspace = true
serde = { workspace = true, feat = ["derive"] }
starknet_mempool_infra = { path = "../mempool_infra" }
thiserror.workspace = true
9 changes: 9 additions & 0 deletions crates/batcher_types/src/batcher_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};

use crate::errors::BatcherError;

// TODO(Tsabary/Yael/Dafna): Populate the data structure used to invoke the batcher.
#[derive(Debug, Serialize, Deserialize)]
pub struct BatcherInput {}

pub type BatcherResult<T> = Result<T, BatcherError>;
88 changes: 88 additions & 0 deletions crates/batcher_types/src/communication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::sync::Arc;

use async_trait::async_trait;
use mockall::predicate::*;
use mockall::*;
use serde::{Deserialize, Serialize};
use starknet_mempool_infra::component_client::{
ClientError,
LocalComponentClient,
RemoteComponentClient,
};
use starknet_mempool_infra::component_definitions::ComponentRequestAndResponseSender;
use thiserror::Error;

use crate::batcher_types::{BatcherInput, BatcherResult};
use crate::errors::BatcherError;
pub type BatcherClientImpl = LocalComponentClient<BatcherRequest, BatcherResponse>;
pub type RemoteBatcherClientImpl = RemoteComponentClient<BatcherRequest, BatcherResponse>;
pub type BatcherClientResult<T> = Result<T, BatcherClientError>;
pub type BatcherRequestAndResponseSender =
ComponentRequestAndResponseSender<BatcherRequest, BatcherResponse>;
pub type SharedBatcherClient = Arc<dyn BatcherClient>;

// TODO(Tsabary/Yael/Dafna): Replace with the actual return type of the batcher function.
pub type PlaceholderReturnType = ();

/// Serves as the batcher's shared interface. Requires `Send + Sync` to allow transferring and
/// sharing resources (inputs, futures) across threads.
#[automock]
#[async_trait]
pub trait BatcherClient: Send + Sync {
async fn batcher_placeholder_fn_name(
&self,
batcher_input: BatcherInput,
) -> BatcherClientResult<PlaceholderReturnType>;
}

#[derive(Debug, Serialize, Deserialize)]
pub enum BatcherRequest {
PlaceholderBatcherRequest(BatcherInput),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum BatcherResponse {
PlaceholderBatcherResponse(BatcherResult<PlaceholderReturnType>),
}

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

#[async_trait]
impl BatcherClient for BatcherClientImpl {
async fn batcher_placeholder_fn_name(
&self,
batcher_input: BatcherInput,
) -> BatcherClientResult<PlaceholderReturnType> {
let request = BatcherRequest::PlaceholderBatcherRequest(batcher_input);
let response = self.send(request).await;
match response {
BatcherResponse::PlaceholderBatcherResponse(Ok(response)) => Ok(response),
BatcherResponse::PlaceholderBatcherResponse(Err(response)) => {
Err(BatcherClientError::BatcherError(response))
}
}
}
}

#[async_trait]
impl BatcherClient for RemoteBatcherClientImpl {
async fn batcher_placeholder_fn_name(
&self,
batcher_input: BatcherInput,
) -> BatcherClientResult<PlaceholderReturnType> {
let request = BatcherRequest::PlaceholderBatcherRequest(batcher_input);
let response = self.send(request).await?;
match response {
BatcherResponse::PlaceholderBatcherResponse(Ok(response)) => Ok(response),
BatcherResponse::PlaceholderBatcherResponse(Err(response)) => {
Err(BatcherClientError::BatcherError(response))
}
}
}
}
9 changes: 9 additions & 0 deletions crates/batcher_types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};
use thiserror::Error;

// TODO(Tsabary/Yael/Dafna): Populate with actual errors.
#[derive(Clone, Debug, Error, PartialEq, Eq, Serialize, Deserialize)]
pub enum BatcherError {
#[error("Placeholder error message")]
Placeholder,
}
4 changes: 3 additions & 1 deletion crates/batcher_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

pub mod batcher_types;
pub mod communication;
pub mod errors;

0 comments on commit ad45c95

Please sign in to comment.