Skip to content

Commit

Permalink
feat: add a proxy mempool example test
Browse files Browse the repository at this point in the history
  • Loading branch information
uriel-starkware committed Apr 11, 2024
1 parent 72a2b06 commit 0cb8df0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 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.

1 change: 1 addition & 0 deletions crates/mempool_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ workspace = true

[dependencies]
tokio.workspace = true
async-trait = "0.1.79"
6 changes: 6 additions & 0 deletions crates/mempool_node/src/main.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
30 changes: 30 additions & 0 deletions crates/mempool_node/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
}

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
}
}
60 changes: 60 additions & 0 deletions crates/mempool_node/src/proxy_mempool.rs
Original file line number Diff line number Diff line change
@@ -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<ProxyFunc>,
rx_ret_value: Receiver<ProxyRetValue>,
}

impl ProxyMempool {
pub fn new(mempool: Arc<Mutex<Mempool>>) -> 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,
}
}
}
18 changes: 18 additions & 0 deletions crates/mempool_node/src/proxy_mempool_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 0cb8df0

Please sign in to comment.