Skip to content

Commit

Permalink
feat(net): backchannel peers
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Aug 30, 2024
1 parent 5f0c9ca commit 706f230
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions crates/net/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::{
};
use alloy::primitives::Address;
use eyre::Result;
use tracing::error;
use std::sync::mpsc::Receiver;
use tokio::{select, sync::watch};
use crate::types::peer::Peer;

/// NetworkDriver
///
Expand Down Expand Up @@ -44,14 +46,21 @@ impl NetworkDriver {

/// Starts the Discv5 peer discovery & libp2p services
/// and continually listens for new peers and messages to handle
pub fn start(mut self) -> Result<()> {
pub fn start(mut self) -> Result<Receiver<Peer>> {
let mut peer_recv = self.discovery.start()?;
let (backchannel_peer_send, backchannel_peer_recv) = std::sync::mpsc::channel::<Peer>();
self.gossip.listen()?;
tokio::spawn(async move {
loop {
select! {
peer = peer_recv.recv() => {
self.gossip.dial_opt(peer).await;
self.gossip.dial_opt(peer.clone()).await;
let Some(peer) = peer else {
break;
};
if let Err(e) = backchannel_peer_send.send(peer.clone()) {
error!("Failed to send peer to backchannel: {:?}", e);
}
},
event = self.gossip.select_next_some() => {
self.gossip.handle_event(event);
Expand All @@ -60,6 +69,6 @@ impl NetworkDriver {
}
});

Ok(())
Ok(backchannel_peer_recv)
}
}
2 changes: 1 addition & 1 deletion crates/net/src/types/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libp2p::{multiaddr::Protocol, Multiaddr};
use std::net::{IpAddr, SocketAddr};

/// A wrapper around a peer's [SocketAddr].
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Peer {
/// The peer's [SocketAddr].
pub socket: SocketAddr,
Expand Down

0 comments on commit 706f230

Please sign in to comment.