diff --git a/Cargo.lock b/Cargo.lock index 3da4aa303..5ebcb73f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1283,6 +1283,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" name = "mempool_node" version = "0.0.0" dependencies = [ + "async-trait", "tokio", ] diff --git a/crates/mempool_node/Cargo.toml b/crates/mempool_node/Cargo.toml index c309528de..940664606 100644 --- a/crates/mempool_node/Cargo.toml +++ b/crates/mempool_node/Cargo.toml @@ -10,3 +10,4 @@ workspace = true [dependencies] tokio.workspace = true +async-trait = "0.1.79" \ No newline at end of file diff --git a/crates/mempool_node/src/main.rs b/crates/mempool_node/src/main.rs index 9668ab695..d903e7660 100644 --- a/crates/mempool_node/src/main.rs +++ b/crates/mempool_node/src/main.rs @@ -1,3 +1,9 @@ +pub mod mempool; +pub mod proxy_mempool; + +#[cfg(test)] +mod proxy_mempool_test; + #[tokio::main] async fn main() { let my_string = "Main function placeholder"; diff --git a/crates/mempool_node/src/mempool.rs b/crates/mempool_node/src/mempool.rs new file mode 100644 index 000000000..0bda0294b --- /dev/null +++ b/crates/mempool_node/src/mempool.rs @@ -0,0 +1,30 @@ +use async_trait::async_trait; + +pub type AddTransactionCallType = u32; +pub type AddTransactionReturnType = bool; + +#[async_trait] +pub trait MempoolTrait { + async fn add_transaction(&mut self, trx: AddTransactionCallType) -> AddTransactionReturnType; +} + +#[derive(Default)] +pub struct Mempool { + transactions: Vec, +} + +impl Mempool { + pub fn new() -> Self { + Self { + transactions: vec![], + } + } +} + +#[async_trait] +impl MempoolTrait for Mempool { + async fn add_transaction(&mut self, trx: u32) -> bool { + self.transactions.push(trx); + true + } +} diff --git a/crates/mempool_node/src/proxy_mempool.rs b/crates/mempool_node/src/proxy_mempool.rs new file mode 100644 index 000000000..84101c9d0 --- /dev/null +++ b/crates/mempool_node/src/proxy_mempool.rs @@ -0,0 +1,60 @@ +use crate::mempool::{AddTransactionCallType, AddTransactionReturnType, Mempool, MempoolTrait}; +use async_trait::async_trait; +use std::sync::Arc; + +use tokio::sync::mpsc::{channel, Receiver, Sender}; +use tokio::sync::Mutex; +use tokio::task; + +enum ProxyFunc { + AddTransaction(u32), +} + +enum ProxyRetValue { + AddTransaction(bool), +} + +pub struct ProxyMempool { + tx_call: Sender, + rx_ret_value: Receiver, +} + +impl ProxyMempool { + pub fn new(mempool: Arc>) -> Self { + let (tx_call, mut rx_call) = channel(32); + let (tx_ret_value, rx_ret_value) = channel(32); + + task::spawn(async move { + while let Some(call) = rx_call.recv().await { + match call { + ProxyFunc::AddTransaction(trx) => { + let ret_value = mempool.lock().await.add_transaction(trx).await; + tx_ret_value + .send(ProxyRetValue::AddTransaction(ret_value)) + .await + .unwrap(); + } + } + } + }); + + ProxyMempool { + tx_call, + rx_ret_value, + } + } +} + +#[async_trait] +impl MempoolTrait for ProxyMempool { + async fn add_transaction(&mut self, trx: AddTransactionCallType) -> AddTransactionReturnType { + self.tx_call + .send(ProxyFunc::AddTransaction(trx)) + .await + .unwrap(); + match self.rx_ret_value.recv().await { + Some(ProxyRetValue::AddTransaction(b)) => b, + None => false, + } + } +} diff --git a/crates/mempool_node/src/proxy_mempool_test.rs b/crates/mempool_node/src/proxy_mempool_test.rs new file mode 100644 index 000000000..5b46b3d36 --- /dev/null +++ b/crates/mempool_node/src/proxy_mempool_test.rs @@ -0,0 +1,18 @@ +mod test { + use std::sync::Arc; + + use tokio::sync::Mutex; + + use crate::{ + mempool::{self, MempoolTrait}, + proxy_mempool, + }; + + #[tokio::test] + async fn test_proxy_add_transaction() { + let mempool = Arc::new(Mutex::new(mempool::Mempool::new())); + let mut proxy = proxy_mempool::ProxyMempool::new(mempool); + + assert!(proxy.add_transaction(1).await); + } +}