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

chore(sequencer_infra): add component client trait for local client #2084

Merged
merged 1 commit into from
Nov 17, 2024
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
2 changes: 1 addition & 1 deletion crates/papyrus_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn handle_response_variants(input: TokenStream) -> TokenStream {
} = parse_macro_input!(input as HandleResponseVariantsMacroInput);

let expanded = quote! {
match response {
match response? {
#response_enum::#request_response_enum_var(Ok(response)) => Ok(response),
#response_enum::#request_response_enum_var(Err(response)) => {
Err(#component_client_error::#component_error(response))
Expand Down
29 changes: 16 additions & 13 deletions crates/starknet_batcher_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use starknet_sequencer_infra::component_client::{
LocalComponentClient,
RemoteComponentClient,
};
use starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
use starknet_sequencer_infra::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
};
use thiserror::Error;

use crate::batcher_types::{
Expand Down Expand Up @@ -100,7 +103,7 @@ pub enum BatcherClientError {
impl BatcherClient for LocalBatcherClient {
async fn build_proposal(&self, input: BuildProposalInput) -> BatcherClientResult<()> {
let request = BatcherRequest::BuildProposal(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, BuildProposal, BatcherClientError, BatcherError)
}

Expand All @@ -109,7 +112,7 @@ impl BatcherClient for LocalBatcherClient {
input: GetProposalContentInput,
) -> BatcherClientResult<GetProposalContentResponse> {
let request = BatcherRequest::GetProposalContent(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
GetProposalContent,
Expand All @@ -120,7 +123,7 @@ impl BatcherClient for LocalBatcherClient {

async fn validate_proposal(&self, input: ValidateProposalInput) -> BatcherClientResult<()> {
let request = BatcherRequest::ValidateProposal(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
ValidateProposal,
Expand All @@ -134,7 +137,7 @@ impl BatcherClient for LocalBatcherClient {
input: SendProposalContentInput,
) -> BatcherClientResult<SendProposalContentResponse> {
let request = BatcherRequest::SendProposalContent(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
SendProposalContent,
Expand All @@ -145,13 +148,13 @@ impl BatcherClient for LocalBatcherClient {

async fn start_height(&self, input: StartHeightInput) -> BatcherClientResult<()> {
let request = BatcherRequest::StartHeight(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, StartHeight, BatcherClientError, BatcherError)
}

async fn decision_reached(&self, input: DecisionReachedInput) -> BatcherClientResult<()> {
let request = BatcherRequest::DecisionReached(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
DecisionReached,
Expand All @@ -165,7 +168,7 @@ impl BatcherClient for LocalBatcherClient {
impl BatcherClient for RemoteBatcherClient {
async fn build_proposal(&self, input: BuildProposalInput) -> BatcherClientResult<()> {
let request = BatcherRequest::BuildProposal(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, BuildProposal, BatcherClientError, BatcherError)
}

Expand All @@ -174,7 +177,7 @@ impl BatcherClient for RemoteBatcherClient {
input: GetProposalContentInput,
) -> BatcherClientResult<GetProposalContentResponse> {
let request = BatcherRequest::GetProposalContent(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
GetProposalContent,
Expand All @@ -185,7 +188,7 @@ impl BatcherClient for RemoteBatcherClient {

async fn validate_proposal(&self, input: ValidateProposalInput) -> BatcherClientResult<()> {
let request = BatcherRequest::ValidateProposal(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
ValidateProposal,
Expand All @@ -199,7 +202,7 @@ impl BatcherClient for RemoteBatcherClient {
input: SendProposalContentInput,
) -> BatcherClientResult<SendProposalContentResponse> {
let request = BatcherRequest::SendProposalContent(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
SendProposalContent,
Expand All @@ -210,13 +213,13 @@ impl BatcherClient for RemoteBatcherClient {

async fn start_height(&self, input: StartHeightInput) -> BatcherClientResult<()> {
let request = BatcherRequest::StartHeight(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(BatcherResponse, StartHeight, BatcherClientError, BatcherError)
}

async fn decision_reached(&self, input: DecisionReachedInput) -> BatcherClientResult<()> {
let request = BatcherRequest::DecisionReached(input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
BatcherResponse,
DecisionReached,
Expand Down
9 changes: 6 additions & 3 deletions crates/starknet_gateway_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use starknet_sequencer_infra::component_client::{
LocalComponentClient,
RemoteComponentClient,
};
use starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
use starknet_sequencer_infra::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
};
use thiserror::Error;

use crate::errors::GatewayError;
Expand Down Expand Up @@ -56,7 +59,7 @@ impl GatewayClient for LocalGatewayClient {
#[instrument(skip(self))]
async fn add_tx(&self, gateway_input: GatewayInput) -> GatewayClientResult<TransactionHash> {
let request = GatewayRequest::AddTransaction(gateway_input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(GatewayResponse, AddTransaction, GatewayClientError, GatewayError)
}
}
Expand All @@ -66,7 +69,7 @@ impl GatewayClient for RemoteGatewayClient {
#[instrument(skip(self))]
async fn add_tx(&self, gateway_input: GatewayInput) -> GatewayClientResult<TransactionHash> {
let request = GatewayRequest::AddTransaction(gateway_input);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(GatewayResponse, AddTransaction, GatewayClientError, GatewayError)
}
}
13 changes: 8 additions & 5 deletions crates/starknet_mempool_p2p_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use starknet_sequencer_infra::component_client::{
LocalComponentClient,
RemoteComponentClient,
};
use starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
use starknet_sequencer_infra::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
};
use thiserror::Error;

use crate::errors::MempoolP2pPropagatorError;
Expand Down Expand Up @@ -69,7 +72,7 @@ impl MempoolP2pPropagatorClient for LocalMempoolP2pPropagatorClient {
transaction: RpcTransaction,
) -> MempoolP2pPropagatorClientResult<()> {
let request = MempoolP2pPropagatorRequest::AddTransaction(transaction);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolP2pPropagatorResponse,
AddTransaction,
Expand All @@ -83,7 +86,7 @@ impl MempoolP2pPropagatorClient for LocalMempoolP2pPropagatorClient {
propagation_metadata: BroadcastedMessageMetadata,
) -> MempoolP2pPropagatorClientResult<()> {
let request = MempoolP2pPropagatorRequest::ContinuePropagation(propagation_metadata);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolP2pPropagatorResponse,
ContinuePropagation,
Expand All @@ -100,7 +103,7 @@ impl MempoolP2pPropagatorClient for RemoteMempoolP2pPropagatorClient {
transaction: RpcTransaction,
) -> MempoolP2pPropagatorClientResult<()> {
let request = MempoolP2pPropagatorRequest::AddTransaction(transaction);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolP2pPropagatorResponse,
AddTransaction,
Expand All @@ -114,7 +117,7 @@ impl MempoolP2pPropagatorClient for RemoteMempoolP2pPropagatorClient {
propagation_metadata: BroadcastedMessageMetadata,
) -> MempoolP2pPropagatorClientResult<()> {
let request = MempoolP2pPropagatorRequest::ContinuePropagation(propagation_metadata);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolP2pPropagatorResponse,
ContinuePropagation,
Expand Down
17 changes: 10 additions & 7 deletions crates/starknet_mempool_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use starknet_sequencer_infra::component_client::{
LocalComponentClient,
RemoteComponentClient,
};
use starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
use starknet_sequencer_infra::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
};
use thiserror::Error;

use crate::errors::MempoolError;
Expand Down Expand Up @@ -70,19 +73,19 @@ pub enum MempoolClientError {
impl MempoolClient for LocalMempoolClient {
async fn add_tx(&self, args: AddTransactionArgsWrapper) -> MempoolClientResult<()> {
let request = MempoolRequest::AddTransaction(args);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(MempoolResponse, AddTransaction, MempoolClientError, MempoolError)
}

async fn commit_block(&self, args: CommitBlockArgs) -> MempoolClientResult<()> {
let request = MempoolRequest::CommitBlock(args);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(MempoolResponse, CommitBlock, MempoolClientError, MempoolError)
}

async fn get_txs(&self, n_txs: usize) -> MempoolClientResult<Vec<AccountTransaction>> {
let request = MempoolRequest::GetTransactions(n_txs);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolResponse,
GetTransactions,
Expand All @@ -96,19 +99,19 @@ impl MempoolClient for LocalMempoolClient {
impl MempoolClient for RemoteMempoolClient {
async fn add_tx(&self, args: AddTransactionArgsWrapper) -> MempoolClientResult<()> {
let request = MempoolRequest::AddTransaction(args);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(MempoolResponse, AddTransaction, MempoolClientError, MempoolError)
}

async fn commit_block(&self, args: CommitBlockArgs) -> MempoolClientResult<()> {
let request = MempoolRequest::CommitBlock(args);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(MempoolResponse, CommitBlock, MempoolClientError, MempoolError)
}

async fn get_txs(&self, n_txs: usize) -> MempoolClientResult<Vec<AccountTransaction>> {
let request = MempoolRequest::GetTransactions(n_txs);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(
MempoolResponse,
GetTransactions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::any::type_name;

use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tokio::sync::mpsc::{channel, Sender};
use tracing::info;

use crate::component_client::ClientResult;
use crate::component_definitions::ComponentRequestAndResponseSender;
use crate::component_definitions::{ComponentClient, ComponentRequestAndResponseSender};

/// The `LocalComponentClient` struct is a generic client for sending component requests and
/// receiving responses asynchronously.
Expand All @@ -20,16 +23,22 @@ use crate::component_definitions::ComponentRequestAndResponseSender;
/// # Example
/// ```rust
/// // Example usage of the LocalComponentClient
/// use serde::{Deserialize, Serialize};
/// use tokio::sync::mpsc::Sender;
///
/// use crate::starknet_sequencer_infra::component_client::LocalComponentClient;
/// use crate::starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
/// use crate::starknet_sequencer_infra::component_definitions::{
/// ComponentClient,
/// ComponentRequestAndResponseSender,
/// };
///
/// // Define your request and response types
/// #[derive(Deserialize, Serialize)]
/// struct MyRequest {
/// pub content: String,
/// }
///
/// #[derive(Deserialize, Serialize)]
/// struct MyResponse {
/// content: String,
/// }
Expand Down Expand Up @@ -71,8 +80,16 @@ where
pub fn new(tx: Sender<ComponentRequestAndResponseSender<Request, Response>>) -> Self {
Self { tx }
}
}

pub async fn send(&self, request: Request) -> ClientResult<Response> {
#[async_trait]
impl<Request, Response> ComponentClient<Request, Response>
for LocalComponentClient<Request, Response>
where
Request: Send + Sync + Serialize + DeserializeOwned,
Response: Send + Sync + Serialize + DeserializeOwned,
{
async fn send(&self, request: Request) -> ClientResult<Response> {
let (res_tx, mut res_rx) = channel::<Response>(1);
let request_and_res_tx = ComponentRequestAndResponseSender { request, tx: res_tx };
self.tx.send(request_and_res_tx).await.expect("Outbound connection should be open.");
Expand Down
11 changes: 11 additions & 0 deletions crates/starknet_sequencer_infra/src/component_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use async_trait::async_trait;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{error, info};
use validator::Validate;

use crate::component_client::ClientResult;
use crate::errors::ComponentError;

pub const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";
Expand All @@ -25,6 +27,15 @@ pub trait ComponentRequestHandler<Request, Response> {
async fn handle_request(&mut self, request: Request) -> Response;
}

#[async_trait]
pub trait ComponentClient<Request, Response>
where
Request: Send + Sync + Serialize + DeserializeOwned,
Response: Send + Sync + Serialize + DeserializeOwned,
{
async fn send(&self, request: Request) -> ClientResult<Response>;
}

#[async_trait]
pub trait ComponentStarter {
async fn start(&mut self) -> Result<(), ComponentError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::component_client::{ClientError, LocalComponentClient};
use crate::component_definitions::{RemoteServerConfig, ServerError, APPLICATION_OCTET_STREAM};
use crate::component_definitions::{
ComponentClient,
RemoteServerConfig,
ServerError,
APPLICATION_OCTET_STREAM,
};
use crate::component_server::ComponentServerStarter;
use crate::errors::ComponentServerError;
use crate::serde_utils::BincodeSerdeWrapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio::sync::mpsc::channel;
use tokio::task;

use crate::component_client::{ClientError, ClientResult, LocalComponentClient};
use crate::component_definitions::ComponentRequestAndResponseSender;
use crate::component_definitions::{ComponentClient, ComponentRequestAndResponseSender};
use crate::component_server::{ComponentServerStarter, LocalComponentServer};
use crate::tests::{
test_a_b_functionality,
Expand Down
7 changes: 5 additions & 2 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use starknet_sequencer_infra::component_client::{
LocalComponentClient,
RemoteComponentClient,
};
use starknet_sequencer_infra::component_definitions::ComponentRequestAndResponseSender;
use starknet_sequencer_infra::component_definitions::{
ComponentClient,
ComponentRequestAndResponseSender,
};
use thiserror::Error;

use crate::errors::StateSyncError;
Expand Down Expand Up @@ -73,7 +76,7 @@ impl StateSyncClient for RemoteStateSyncClient {
block_number: BlockNumber,
) -> StateSyncClientResult<Option<SyncBlock>> {
let request = StateSyncRequest::GetBlock(block_number);
let response = self.send(request).await?;
let response = self.send(request).await;
handle_response_variants!(StateSyncResponse, GetBlock, StateSyncClientError, StateSyncError)
}
}
Loading