2023-02-21 08:10:26 +00:00
|
|
|
use nomos_core::{
|
|
|
|
block::BlockId,
|
2023-03-14 16:55:08 +00:00
|
|
|
tx::mock::{MockTransaction, MockTxId},
|
2023-02-21 08:10:26 +00:00
|
|
|
};
|
2023-02-07 09:13:22 +00:00
|
|
|
use nomos_log::{Logger, LoggerSettings};
|
|
|
|
use nomos_network::{
|
2023-02-21 08:10:26 +00:00
|
|
|
backends::mock::{Mock, MockBackendMessage, MockConfig, MockMessage},
|
2023-02-07 09:13:22 +00:00
|
|
|
NetworkConfig, NetworkMsg, NetworkService,
|
|
|
|
};
|
|
|
|
use overwatch_derive::*;
|
|
|
|
use overwatch_rs::{overwatch::OverwatchRunner, services::handle::ServiceHandle};
|
|
|
|
|
|
|
|
use nomos_mempool::{
|
|
|
|
backend::mockpool::MockPool,
|
2023-02-21 08:10:26 +00:00
|
|
|
network::adapters::mock::{MockAdapter, MOCK_TX_CONTENT_TOPIC},
|
2023-02-07 09:13:22 +00:00
|
|
|
MempoolMsg, MempoolService,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Services)]
|
|
|
|
struct MockPoolNode {
|
|
|
|
logging: ServiceHandle<Logger>,
|
|
|
|
network: ServiceHandle<NetworkService<Mock>>,
|
2023-03-14 16:55:08 +00:00
|
|
|
mockpool: ServiceHandle<MempoolService<MockAdapter, MockPool<MockTxId, MockTransaction>>>,
|
2023-02-07 09:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mockmempool() {
|
|
|
|
let exist = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
|
|
|
let exist2 = exist.clone();
|
|
|
|
|
|
|
|
let predefined_messages = vec![
|
|
|
|
MockMessage {
|
|
|
|
payload: "This is foo".to_string(),
|
2023-02-21 08:10:26 +00:00
|
|
|
content_topic: MOCK_TX_CONTENT_TOPIC,
|
2023-02-07 09:13:22 +00:00
|
|
|
version: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
MockMessage {
|
|
|
|
payload: "This is bar".to_string(),
|
2023-02-21 08:10:26 +00:00
|
|
|
content_topic: MOCK_TX_CONTENT_TOPIC,
|
2023-02-07 09:13:22 +00:00
|
|
|
version: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let exp_txns = predefined_messages
|
|
|
|
.iter()
|
2023-02-21 08:10:26 +00:00
|
|
|
.cloned()
|
2023-02-07 09:13:22 +00:00
|
|
|
.collect::<std::collections::HashSet<_>>();
|
|
|
|
|
|
|
|
let app = OverwatchRunner::<MockPoolNode>::run(
|
|
|
|
MockPoolNodeServiceSettings {
|
|
|
|
network: NetworkConfig {
|
|
|
|
backend: MockConfig {
|
|
|
|
predefined_messages,
|
|
|
|
duration: tokio::time::Duration::from_millis(100),
|
|
|
|
seed: 0,
|
|
|
|
version: 1,
|
|
|
|
weights: None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mockpool: (),
|
|
|
|
logging: LoggerSettings::default(),
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.map_err(|e| eprintln!("Error encountered: {}", e))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let network = app.handle().relay::<NetworkService<Mock>>();
|
|
|
|
let mempool = app
|
|
|
|
.handle()
|
2023-03-14 16:55:08 +00:00
|
|
|
.relay::<MempoolService<MockAdapter, MockPool<MockTxId, MockTransaction>>>();
|
2023-02-07 09:13:22 +00:00
|
|
|
|
|
|
|
app.spawn(async move {
|
|
|
|
let network_outbound = network.connect().await.unwrap();
|
|
|
|
let mempool_outbound = mempool.connect().await.unwrap();
|
|
|
|
|
|
|
|
// subscribe to the mock content topic
|
|
|
|
network_outbound
|
|
|
|
.send(NetworkMsg::Process(MockBackendMessage::RelaySubscribe {
|
2023-03-14 16:55:08 +00:00
|
|
|
topic: MOCK_TX_CONTENT_TOPIC.content_topic_name.to_string(),
|
2023-02-07 09:13:22 +00:00
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// try to wait all txs to be stored in mempool
|
|
|
|
loop {
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
|
|
|
let (mtx, mrx) = tokio::sync::oneshot::channel();
|
|
|
|
mempool_outbound
|
|
|
|
.send(MempoolMsg::View {
|
2023-02-10 11:04:56 +00:00
|
|
|
ancestor_hint: BlockId::default(),
|
2023-02-08 10:07:09 +00:00
|
|
|
reply_channel: mtx,
|
2023-02-07 09:13:22 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let items = mrx
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2023-03-14 16:55:08 +00:00
|
|
|
.map(|msg| msg.message().clone())
|
2023-02-07 09:13:22 +00:00
|
|
|
.collect::<std::collections::HashSet<_>>();
|
|
|
|
|
|
|
|
if items.len() == exp_txns.len() {
|
|
|
|
assert_eq!(exp_txns, items);
|
|
|
|
exist.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
while !exist2.load(std::sync::atomic::Ordering::SeqCst) {
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(200));
|
|
|
|
}
|
|
|
|
}
|