2023-04-24 02:50:03 -04:00
|
|
|
use core::time::Duration;
|
2023-09-25 19:28:53 -04:00
|
|
|
use std::sync::Arc;
|
2023-04-24 02:50:03 -04:00
|
|
|
|
|
|
|
|
use rand_core::OsRng;
|
|
|
|
|
|
2023-09-25 19:28:53 -04:00
|
|
|
use tokio::{sync::broadcast, time::sleep};
|
2023-04-24 02:50:03 -04:00
|
|
|
|
|
|
|
|
use serai_db::MemDb;
|
|
|
|
|
|
|
|
|
|
use tributary::Tributary;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
tributary::Transaction,
|
2023-10-14 14:56:02 -04:00
|
|
|
ActiveTributary, TributaryEvent,
|
2023-10-13 22:40:11 -04:00
|
|
|
p2p::handle_p2p_task,
|
2023-08-08 15:12:47 -04:00
|
|
|
tests::{
|
|
|
|
|
LocalP2p,
|
|
|
|
|
tributary::{new_keys, new_spec, new_tributaries},
|
|
|
|
|
},
|
2023-04-24 02:50:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn handle_p2p_test() {
|
|
|
|
|
let keys = new_keys(&mut OsRng);
|
|
|
|
|
let spec = new_spec(&mut OsRng, &keys);
|
|
|
|
|
|
|
|
|
|
let mut tributaries = new_tributaries(&keys, &spec).await;
|
|
|
|
|
|
2023-09-25 19:28:53 -04:00
|
|
|
let mut tributary_senders = vec![];
|
2023-04-24 02:50:03 -04:00
|
|
|
let mut tributary_arcs = vec![];
|
2023-10-14 14:57:46 -04:00
|
|
|
for (p2p, tributary) in tributaries.drain(..) {
|
2023-09-25 19:28:53 -04:00
|
|
|
let tributary = Arc::new(tributary);
|
2023-04-24 02:50:03 -04:00
|
|
|
tributary_arcs.push(tributary.clone());
|
2023-09-25 19:28:53 -04:00
|
|
|
let (new_tributary_send, new_tributary_recv) = broadcast::channel(5);
|
2023-10-14 14:57:46 -04:00
|
|
|
tokio::spawn(handle_p2p_task(p2p, new_tributary_recv));
|
2023-09-25 19:28:53 -04:00
|
|
|
new_tributary_send
|
2023-10-14 14:56:02 -04:00
|
|
|
.send(TributaryEvent::NewTributary(ActiveTributary { spec: spec.clone(), tributary }))
|
2023-09-25 19:28:53 -04:00
|
|
|
.map_err(|_| "failed to send ActiveTributary")
|
|
|
|
|
.unwrap();
|
|
|
|
|
tributary_senders.push(new_tributary_send);
|
2023-04-24 02:50:03 -04:00
|
|
|
}
|
|
|
|
|
let tributaries = tributary_arcs;
|
|
|
|
|
|
|
|
|
|
// After two blocks of time, we should have a new block
|
|
|
|
|
// We don't wait one block of time as we may have missed the chance for this block
|
|
|
|
|
sleep(Duration::from_secs((2 * Tributary::<MemDb, Transaction, LocalP2p>::block_time()).into()))
|
|
|
|
|
.await;
|
2023-09-25 19:28:53 -04:00
|
|
|
let tip = tributaries[0].tip().await;
|
2023-04-24 02:50:03 -04:00
|
|
|
assert!(tip != spec.genesis());
|
|
|
|
|
|
|
|
|
|
// Sleep one second to make sure this block propagates
|
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
|
|
|
|
// Make sure every tributary has it
|
|
|
|
|
for tributary in &tributaries {
|
2023-09-25 19:28:53 -04:00
|
|
|
assert!(tributary.reader().block(&tip).is_some());
|
2023-04-24 02:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then after another block of time, we should have yet another new block
|
|
|
|
|
sleep(Duration::from_secs(Tributary::<MemDb, Transaction, LocalP2p>::block_time().into())).await;
|
2023-09-25 19:28:53 -04:00
|
|
|
let new_tip = tributaries[0].tip().await;
|
2023-04-24 02:50:03 -04:00
|
|
|
assert!(new_tip != tip);
|
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
|
|
|
|
for tributary in tributaries {
|
2023-09-25 19:28:53 -04:00
|
|
|
assert!(tributary.reader().block(&new_tip).is_some());
|
2023-04-24 02:50:03 -04:00
|
|
|
}
|
|
|
|
|
}
|