From 706f230776e40581126ff553d16cdb14a92dcd77 Mon Sep 17 00:00:00 2001 From: refcell Date: Thu, 29 Aug 2024 20:54:38 -0400 Subject: [PATCH] feat(net): backchannel peers --- crates/net/src/driver.rs | 15 ++++++++++++--- crates/net/src/types/peer.rs | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/net/src/driver.rs b/crates/net/src/driver.rs index 191312f..166a440 100644 --- a/crates/net/src/driver.rs +++ b/crates/net/src/driver.rs @@ -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 /// @@ -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> { let mut peer_recv = self.discovery.start()?; + let (backchannel_peer_send, backchannel_peer_recv) = std::sync::mpsc::channel::(); 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); @@ -60,6 +69,6 @@ impl NetworkDriver { } }); - Ok(()) + Ok(backchannel_peer_recv) } } diff --git a/crates/net/src/types/peer.rs b/crates/net/src/types/peer.rs index a7fb1fb..596fea5 100644 --- a/crates/net/src/types/peer.rs +++ b/crates/net/src/types/peer.rs @@ -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,