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 4, 2024
1 parent 770e2ed commit 0674c70
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

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


[dependencies]

[lints]
workspace = true

[dependencies]
async-trait.workspace = true
mockall.workspace = true
papyrus_proc_macros = { path = "../papyrus_proc_macros" }
serde = { workspace = true, feat = ["derive"] }
starknet_mempool_infra = { path = "../mempool_infra" }
thiserror.workspace = true
21 changes: 21 additions & 0 deletions crates/batcher_types/src/batcher_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 BatcherFnOneInput {}

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

// TODO(Tsabary/Yael/Dafna): Replace with the actual return type of the batcher function.
#[derive(Debug, Serialize, Deserialize)]
pub struct BatcherFnOneReturnValue {}

// TODO(Tsabary/Yael/Dafna): Replace with the actual return type of the batcher function.
#[derive(Debug, Serialize, Deserialize)]
pub struct BatcherFnTwoReturnValue {}

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

use async_trait::async_trait;
use mockall::predicate::*;
use mockall::*;
use papyrus_proc_macros::handle_response_variants;
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::{
BatcherFnOneInput,
BatcherFnOneReturnValue,
BatcherFnTwoInput,
BatcherFnTwoReturnValue,
BatcherResult,
};
use crate::errors::BatcherError;

pub type LocalBatcherClientImpl = 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>;

/// 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_fn_one(
&self,
batcher_fn_one_input: BatcherFnOneInput,
) -> BatcherClientResult<BatcherFnOneReturnValue>;

async fn batcher_fn_two(
&self,
batcher_fn_two_input: BatcherFnTwoInput,
) -> BatcherClientResult<BatcherFnTwoReturnValue>;
}

#[derive(Debug, Serialize, Deserialize)]
pub enum BatcherRequest {
BatcherFnOne(BatcherFnOneInput),
BatcherFnTwo(BatcherFnTwoInput),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum BatcherResponse {
BatcherFnOne(BatcherResult<BatcherFnOneReturnValue>),
BatcherFnTwo(BatcherResult<BatcherFnTwoReturnValue>),
}

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

#[async_trait]
impl BatcherClient for LocalBatcherClientImpl {
async fn batcher_fn_one(
&self,
batcher_fn_one_input: BatcherFnOneInput,
) -> BatcherClientResult<BatcherFnOneReturnValue> {
let request = BatcherRequest::BatcherFnOne(batcher_fn_one_input);
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, BatcherFnOne, BatcherClientError, BatcherError)
}

async fn batcher_fn_two(
&self,
batcher_fn_two_input: BatcherFnTwoInput,
) -> BatcherClientResult<BatcherFnTwoReturnValue> {
let request = BatcherRequest::BatcherFnTwo(batcher_fn_two_input);
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, BatcherFnTwo, BatcherClientError, BatcherError)
}
}

#[async_trait]
impl BatcherClient for RemoteBatcherClientImpl {
async fn batcher_fn_one(
&self,
batcher_fn_one_input: BatcherFnOneInput,
) -> BatcherClientResult<BatcherFnOneReturnValue> {
let request = BatcherRequest::BatcherFnOne(batcher_fn_one_input);
let response = self.send(request).await?;
handle_response_variants!(BatcherResponse, BatcherFnOne, BatcherClientError, BatcherError)
}

async fn batcher_fn_two(
&self,
batcher_fn_two_input: BatcherFnTwoInput,
) -> BatcherClientResult<BatcherFnTwoReturnValue> {
let request = BatcherRequest::BatcherFnTwo(batcher_fn_two_input);
let response = self.send(request).await?;
handle_response_variants!(BatcherResponse, BatcherFnTwo, BatcherClientError, BatcherError)
}
}
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 0674c70

Please sign in to comment.