-
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 proxy mempool example test
- Loading branch information
1 parent
72a2b06
commit 0cb8df0
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -10,3 +10,4 @@ workspace = true | |
|
||
[dependencies] | ||
tokio.workspace = true | ||
async-trait = "0.1.79" |
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |