Skip to content

Commit

Permalink
Add forward to bus from network test (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley authored Aug 27, 2024
1 parent 1ed022c commit baaa352
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
use crate::{
ciphernode::Ciphernode,
committee::CommitteeManager,
data::Data,
data::{Data, GetLog},
eventbus::{EventBus, GetHistory, Subscribe},
events::{ComputationRequested, E3id, EnclaveEvent, KeyshareCreated, PublicKeyAggregated},
fhe::{Fhe, WrappedPublicKey, WrappedPublicKeyShare},
Expand Down Expand Up @@ -211,12 +211,13 @@ mod tests {
}

#[actix::test]
async fn test_p2p_event_broadcasting() -> Result<()> {
async fn test_p2p_actor_forwards_events_to_network() -> Result<()> {
// Setup elements in test
let (tx, mut output) = channel(100); // Transmit byte events to the network
let (_, rx) = channel(100); // Receive byte events from the network
let bus = EventBus::new(true).start();
let _ = P2p::spawn_and_listen(bus.clone(), tx.clone(), rx);
P2p::spawn_and_listen(bus.clone(), tx.clone(), rx);

// Capture messages from output on msgs vec
let msgs: Arc<Mutex<Vec<Vec<u8>>>> = Arc::new(Mutex::new(Vec::new()));
let msgs_loop = msgs.clone();
Expand All @@ -243,10 +244,43 @@ mod tests {

bus.do_send(evt_1.clone());
bus.do_send(evt_2.clone());


sleep(Duration::from_millis(1)).await; // need to push to next tick

assert_eq!(
*msgs.lock().await,
vec![evt_1.to_bytes()?, evt_2.to_bytes()?],
"P2p did not transmit events to the network"
);

Ok(())
}

#[actix::test]
async fn test_p2p_actor_forwards_events_to_bus() -> Result<()> {
// Setup elements in test
let (tx, _) = channel(100); // Transmit byte events to the network
let (input, rx) = channel(100); // Receive byte events from the network
let bus = EventBus::new(true).start();
P2p::spawn_and_listen(bus.clone(), tx.clone(), rx);

// Capture messages from output on msgs vec
let event = EnclaveEvent::from(ComputationRequested {
e3_id: E3id::new("1235"),
nodecount: 3,
threshold: 123,
sortition_seed: 123,
});

// lets send an event from the network
let _ = input.send(event.to_bytes()?).await;

sleep(Duration::from_millis(1)).await; // need to push to next tick

assert_eq!(*msgs.lock().await, vec![evt_1.to_bytes()?, evt_2.to_bytes()?], "P2p did not transmit events to the network");
// check the history of the event bus
let history = bus.send(GetHistory).await?;

assert_eq!(history, vec![event]);

Ok(())
}
Expand Down

0 comments on commit baaa352

Please sign in to comment.