Skip to content

Commit

Permalink
feat: add function to create all channels
Browse files Browse the repository at this point in the history
commit-id:4cc36c62
  • Loading branch information
lev-starkware committed Jun 27, 2024
1 parent 8f12d8d commit 956d75c
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/mempool_node/src/communication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use starknet_mempool_types::communication::MempoolRequestAndResponseSender;
use tokio::sync::mpsc::{channel, Receiver, Sender};

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

impl<T: Send + Sync> ComponentCommunication<T> {
fn get_tx(&self) -> Sender<T> {
self.tx.clone()
}
fn get_rx(&mut self) -> Receiver<T> {
self.rx.take().expect("Receiver already taken")
}
}

pub struct MempoolNodeCommunication {
mempool_channel: ComponentCommunication<MempoolRequestAndResponseSender>,
}

impl MempoolNodeCommunication {
pub fn get_mempool_tx(&self) -> Sender<MempoolRequestAndResponseSender> {
self.mempool_channel.get_tx()
}
pub fn get_mempool_rx(&mut self) -> Receiver<MempoolRequestAndResponseSender> {
self.mempool_channel.get_rx()
}
}

pub fn create_node_channels() -> MempoolNodeCommunication {
const MEMPOOL_INVOCATIONS_QUEUE_SIZE: usize = 32;
let (tx_mempool, rx_mempool) =
channel::<MempoolRequestAndResponseSender>(MEMPOOL_INVOCATIONS_QUEUE_SIZE);
MempoolNodeCommunication {
mempool_channel: ComponentCommunication { tx: tx_mempool, rx: Some(rx_mempool) },
}
}

0 comments on commit 956d75c

Please sign in to comment.