Skip to content

Commit

Permalink
refactor: move component communication to infra crate
Browse files Browse the repository at this point in the history
commit-id:4c9d9914
  • Loading branch information
Itay-Tsabary-Starkware committed Jul 7, 2024
1 parent d607289 commit 105e987
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

21 changes: 20 additions & 1 deletion crates/mempool_infra/src/component_definitions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
use async_trait::async_trait;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::{Receiver, Sender};

#[async_trait]
pub trait ComponentRequestHandler<Request, Response> {
async fn handle_request(&mut self, request: Request) -> Response;
}

pub struct ComponentCommunication<T: Send + Sync> {
tx: Option<Sender<T>>,
rx: Option<Receiver<T>>,
}

impl<T: Send + Sync> ComponentCommunication<T> {
pub fn new(tx: Option<Sender<T>>, rx: Option<Receiver<T>>) -> Self {
Self { tx, rx }
}

pub fn take_tx(&self) -> Sender<T> {
self.tx.to_owned().expect("Sender should be available, could be taken only once")
}

pub fn take_rx(&mut self) -> Receiver<T> {
self.rx.take().expect("Receiver should be available, could be taken only once")
}
}

pub struct ComponentRequestAndResponseSender<Request, Response>
where
Request: Send + Sync,
Expand Down
7 changes: 4 additions & 3 deletions crates/mempool_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ workspace = true
[dependencies]
clap.workspace = true
const_format.workspace = true
papyrus_config.workspace = true
serde.workspace = true
starknet_gateway = { path = "../gateway", version = "0.0" }
starknet_mempool = { path = "../mempool", version = "0.0" }
starknet_mempool_infra = { path = "../mempool_infra", version = "0.0" }
starknet_mempool_types = { path = "../mempool_types", version = "0.0" }
serde.workspace = true
papyrus_config.workspace = true
tokio.workspace = true
validator.workspace = true

Expand All @@ -25,4 +26,4 @@ assert_matches.workspace = true
colored.workspace = true
pretty_assertions.workspace = true
serde_json.workspace = true
test_utils = {path = "../test_utils"}
test_utils = { path = "../test_utils" }
17 changes: 2 additions & 15 deletions crates/mempool_node/src/communication.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
use std::sync::Arc;

use starknet_mempool_infra::component_definitions::ComponentCommunication;
use starknet_mempool_types::communication::{
MempoolClientImpl, MempoolRequestAndResponseSender, SharedMempoolClient,
};
use tokio::sync::mpsc::{channel, Receiver, Sender};

use crate::config::MempoolNodeConfig;

pub struct ComponentCommunication<T: Send + Sync> {
tx: Option<Sender<T>>,
rx: Option<Receiver<T>>,
}

impl<T: Send + Sync> ComponentCommunication<T> {
fn take_tx(&self) -> Sender<T> {
self.tx.to_owned().expect("Sender should be available, could be taken only once")
}
fn take_rx(&mut self) -> Receiver<T> {
self.rx.take().expect("Receiver should be available, could be taken only once")
}
}

pub struct MempoolNodeCommunication {
mempool_channel: ComponentCommunication<MempoolRequestAndResponseSender>,
}
Expand All @@ -39,7 +26,7 @@ pub fn create_node_channels() -> MempoolNodeCommunication {
let (tx_mempool, rx_mempool) =
channel::<MempoolRequestAndResponseSender>(MEMPOOL_INVOCATIONS_QUEUE_SIZE);
MempoolNodeCommunication {
mempool_channel: ComponentCommunication { tx: Some(tx_mempool), rx: Some(rx_mempool) },
mempool_channel: ComponentCommunication::new(Some(tx_mempool), Some(rx_mempool)),
}
}

Expand Down

0 comments on commit 105e987

Please sign in to comment.