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

feat: add function to create all channels #261

Closed
wants to merge 1 commit into from
Closed
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
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) },
}
}
Loading