feat: Add PeerEvent to NetworkEvent enum (#574)

* Add peerAdded and peerRemoved events to NetworkEvent enum

* Remove unnecessary todo macros
This commit is contained in:
Sanket Shanbhag
2022-12-24 14:55:09 +05:30
committed by GitHub
parent 7c80dde0ec
commit 43008b7b07
4 changed files with 14 additions and 3 deletions

View File

@@ -51,7 +51,6 @@ use std::{
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing::{error, info, trace, warn};
/// Manages the _entire_ state of the network.
///
/// This is an endless [`Future`] that consistently drives the state of the entire network forward.
@@ -676,6 +675,10 @@ pub enum NetworkEvent {
/// The status of the peer to which a session was established.
status: Status,
},
/// Event emitted when a new peer is added
PeerAdded(PeerId),
/// Event emitted when a new peer is removed
PeerRemoved(PeerId),
}
/// Bundles all listeners for [`NetworkEvent`]s.

View File

@@ -312,6 +312,8 @@ where
msg,
})
}
// TODO Add remaining events
_ => {}
}
}

View File

@@ -65,6 +65,12 @@ async fn test_establish_connections() {
NetworkEvent::SessionEstablished { peer_id, .. } => {
assert!(expected_connections.remove(&peer_id))
}
NetworkEvent::PeerAdded(peer_id) => {
assert!(!expected_connections.contains(&peer_id))
}
NetworkEvent::PeerRemoved(_) => {
panic!("unexpected event")
}
}
}
assert!(expected_connections.is_empty());

View File

@@ -308,7 +308,7 @@ impl NetworkEventStream {
while let Some(ev) = self.inner.next().await {
match ev {
NetworkEvent::SessionClosed { peer_id, reason } => return Some((peer_id, reason)),
NetworkEvent::SessionEstablished { .. } => continue,
_ => continue,
}
}
None
@@ -317,8 +317,8 @@ impl NetworkEventStream {
pub async fn next_session_established(&mut self) -> Option<PeerId> {
while let Some(ev) = self.inner.next().await {
match ev {
NetworkEvent::SessionClosed { .. } => continue,
NetworkEvent::SessionEstablished { peer_id, .. } => return Some(peer_id),
_ => continue,
}
}
None