-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a mempool proxy support for concurrent calls
- Loading branch information
1 parent
a268176
commit 06dff8d
Showing
3 changed files
with
107 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
use async_trait::async_trait; | ||
use tokio::sync::Mutex; | ||
|
||
pub type AddTransactionCallType = u32; | ||
pub type AddTransactionReturnType = usize; | ||
|
||
#[async_trait] | ||
pub trait MempoolTrait { | ||
async fn add_transaction(&mut self, tx: AddTransactionCallType) -> AddTransactionReturnType; | ||
async fn add_transaction(&self, tx: AddTransactionCallType) -> AddTransactionReturnType; | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Mempool { | ||
transactions: Vec<u32>, | ||
transactions: Mutex<Vec<u32>>, | ||
} | ||
|
||
impl Mempool { | ||
pub fn new() -> Self { | ||
Self { | ||
transactions: vec![], | ||
transactions: Mutex::new(vec![]), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl MempoolTrait for Mempool { | ||
async fn add_transaction(&mut self, tx: AddTransactionCallType) -> AddTransactionReturnType { | ||
self.transactions.push(tx); | ||
self.transactions.len() | ||
async fn add_transaction(&self, tx: AddTransactionCallType) -> AddTransactionReturnType { | ||
let mut guarded_transactions = self.transactions.lock().await; | ||
guarded_transactions.push(tx); | ||
guarded_transactions.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,77 @@ | ||
mod tests { | ||
|
||
use std::sync::Arc; | ||
|
||
use tokio::sync::Mutex; | ||
use tokio::task::JoinSet; | ||
|
||
use crate::{ | ||
mempool::{Mempool, MempoolTrait}, | ||
mempool::{AddTransactionCallType, AddTransactionReturnType, Mempool, MempoolTrait}, | ||
mempool_proxy::MempoolProxy, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_proxy_add_transaction() { | ||
let mempool = Arc::new(Mutex::new(Mempool::new())); | ||
let mut proxy = MempoolProxy::new(mempool); | ||
assert_eq!(proxy.add_transaction(1).await, 1); | ||
assert_eq!(proxy.add_transaction(1).await, 2); | ||
async fn test_mempool_simple_add_transaction() { | ||
let mempool = Mempool::default(); | ||
let tx: AddTransactionCallType = 1; | ||
let expected_result: AddTransactionReturnType = 1; | ||
assert_eq!(mempool.add_transaction(tx).await, expected_result); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_proxy_simple_add_transaction() { | ||
let proxy = MempoolProxy::default(); | ||
let tx: AddTransactionCallType = 1; | ||
let expected_result: AddTransactionReturnType = 1; | ||
assert_eq!(proxy.add_transaction(tx).await, expected_result); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_mempool_concurrent_add_transaction() { | ||
let mempool = Arc::new(Mempool::default()); | ||
|
||
let mut tasks: JoinSet<_> = (0..5) | ||
.map(|_| { | ||
let mempool = mempool.clone(); | ||
async move { | ||
let tx: AddTransactionCallType = 1; | ||
mempool.add_transaction(tx).await | ||
} | ||
}) | ||
.collect(); | ||
|
||
let mut results: Vec<AddTransactionReturnType> = vec![]; | ||
while let Some(result) = tasks.join_next().await { | ||
results.push(result.unwrap()); | ||
} | ||
|
||
results.sort(); | ||
|
||
let expected_results: Vec<AddTransactionReturnType> = (1..=5).collect(); | ||
assert_eq!(results, expected_results); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_proxy_concurrent_add_transaction() { | ||
let proxy = MempoolProxy::default(); | ||
|
||
let mut tasks: JoinSet<_> = (0..5) | ||
.map(|_| { | ||
let proxy = proxy.clone(); | ||
async move { | ||
let tx: AddTransactionCallType = 1; | ||
proxy.add_transaction(tx).await | ||
} | ||
}) | ||
.collect(); | ||
|
||
let mut results: Vec<AddTransactionReturnType> = vec![]; | ||
while let Some(result) = tasks.join_next().await { | ||
results.push(result.unwrap()); | ||
} | ||
|
||
results.sort(); | ||
|
||
let expected_results: Vec<AddTransactionReturnType> = (1..=5).collect(); | ||
assert_eq!(results, expected_results); | ||
} | ||
} |