From 8f8a64febcf02a4db879d9b12618c692b95a58ac Mon Sep 17 00:00:00 2001 From: Filip Bozic <70634661+fbozic@users.noreply.github.com> Date: Tue, 21 May 2024 09:38:03 +0200 Subject: [PATCH] chore: fix typos, remove unnecessary clones --- examples/chat/src/network.rs | 29 ++++++++++--------- examples/chat/src/network/events.rs | 2 +- examples/chat/src/network/events/identify.rs | 12 ++++---- examples/chat/src/network/events/relay.rs | 9 ++++-- .../chat/src/network/events/rendezvous.rs | 2 +- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/examples/chat/src/network.rs b/examples/chat/src/network.rs index ea6e8a3..74be451 100644 --- a/examples/chat/src/network.rs +++ b/examples/chat/src/network.rs @@ -9,7 +9,7 @@ use libp2p::{ use multiaddr::Multiaddr; use tokio::sync::{mpsc, oneshot}; use tokio::time; -use tracing::{debug, error, trace, warn}; +use tracing::{debug, error, info, trace, warn}; pub mod client; pub mod events; @@ -38,9 +38,9 @@ pub async fn run( rendezvous_addresses: Vec, ) -> eyre::Result<(NetworkClient, mpsc::Receiver)> { let mut rendezvous = HashMap::new(); - for address in rendezvous_addresses.clone().into_iter() { - let entry = match peak_peer_id(&address) { - Ok(peer_id) => (peer_id, RendezvousEntry::new(address)), + for address in &rendezvous_addresses { + let entry = match peek_peer_id(&address) { + Ok(peer_id) => (peer_id, RendezvousEntry::new(address.clone())), Err(err) => { eyre::bail!("Failed to parse rendezvous PeerId: {}", err); } @@ -49,9 +49,9 @@ pub async fn run( } let mut relays = HashMap::new(); - for address in relay_addresses.clone().into_iter() { - let entry = match peak_peer_id(&address) { - Ok(peer_id) => (peer_id, RelayEntry::new(address)), + for address in &relay_addresses { + let entry = match peek_peer_id(&address) { + Ok(peer_id) => (peer_id, RelayEntry::new(address.clone())), Err(err) => { eyre::bail!("Failed to parse relay PeerId: {}", err); } @@ -88,12 +88,15 @@ async fn run_init_dial( ) { tokio::time::sleep(Duration::from_secs(5)).await; + info!("Initiating dial to rendezvous and relay addresses."); + for addr in rendezvous_addresses .into_iter() .chain(relay_addresses) .collect::>() .into_iter() { + info!("Dialing address: {:?}", addr); if let Err(err) = client.dial(addr).await { error!("Failed to dial rendezvous address: {}", err); }; @@ -172,7 +175,7 @@ pub(crate) struct EventLoop { #[derive(Debug)] pub(crate) struct RelayEntry { address: Multiaddr, - indetify_state: IdentifyState, + identify_state: IdentifyState, reservation_state: RelayReservationState, } @@ -180,7 +183,7 @@ impl RelayEntry { pub(crate) fn new(address: Multiaddr) -> Self { Self { address, - indetify_state: Default::default(), + identify_state: Default::default(), reservation_state: Default::default(), } } @@ -190,7 +193,7 @@ impl RelayEntry { pub(crate) struct RendezvousEntry { address: Multiaddr, cookie: Option, - indetify_state: IdentifyState, + identify_state: IdentifyState, } impl RendezvousEntry { @@ -198,7 +201,7 @@ impl RendezvousEntry { Self { address, cookie: Default::default(), - indetify_state: Default::default(), + identify_state: Default::default(), } } } @@ -271,7 +274,7 @@ impl EventLoop { }; } Command::Dial { peer_addr, sender } => { - let peer_id = match peak_peer_id(&peer_addr) { + let peer_id = match peek_peer_id(&peer_addr) { Ok(peer_id) => peer_id, Err(e) => { let _ = sender.send(Err(eyre::eyre!(e))); @@ -399,7 +402,7 @@ pub(crate) struct MeshPeerInfo { peers: Vec, } -pub(crate) fn peak_peer_id(address: &Multiaddr) -> eyre::Result { +pub(crate) fn peek_peer_id(address: &Multiaddr) -> eyre::Result { match address.iter().last() { Some(proto) => match proto { multiaddr::Protocol::P2p(peer_id) => Ok(peer_id), diff --git a/examples/chat/src/network/events.rs b/examples/chat/src/network/events.rs index 595ae55..92eb35a 100644 --- a/examples/chat/src/network/events.rs +++ b/examples/chat/src/network/events.rs @@ -122,7 +122,7 @@ impl EventLoop { pub(super) async fn handle_rendezvous_discover(&mut self) { for (peer_id, entry) in self.rendezvous.iter() { - if entry.indetify_state.is_exchanged() { + if entry.identify_state.is_exchanged() { self.swarm.behaviour_mut().rendezvous.discover( Some(self.rendezvous_namespace.clone()), entry.cookie.clone(), diff --git a/examples/chat/src/network/events/identify.rs b/examples/chat/src/network/events/identify.rs index 1850d30..d67a7aa 100644 --- a/examples/chat/src/network/events/identify.rs +++ b/examples/chat/src/network/events/identify.rs @@ -11,9 +11,9 @@ impl EventHandler for EventLoop { match event { identify::Event::Received { peer_id, .. } => { if let Some(entry) = self.relays.get_mut(&peer_id) { - entry.indetify_state.received = true; + entry.identify_state.received = true; - if entry.indetify_state.is_exchanged() + if entry.identify_state.is_exchanged() && matches!(entry.reservation_state, RelayReservationState::Unknown) { if let Err(err) = self @@ -27,14 +27,14 @@ impl EventHandler for EventLoop { } if let Some(entry) = self.rendezvous.get_mut(&peer_id) { - entry.indetify_state.received = true; + entry.identify_state.received = true; } } identify::Event::Sent { peer_id } => { if let Some(entry) = self.relays.get_mut(&peer_id) { - entry.indetify_state.sent = true; + entry.identify_state.sent = true; - if entry.indetify_state.is_exchanged() + if entry.identify_state.is_exchanged() && matches!(entry.reservation_state, RelayReservationState::Unknown) { if let Err(err) = self @@ -48,7 +48,7 @@ impl EventHandler for EventLoop { } if let Some(entry) = self.rendezvous.get_mut(&peer_id) { - entry.indetify_state.sent = true; + entry.identify_state.sent = true; } } _ => {} diff --git a/examples/chat/src/network/events/relay.rs b/examples/chat/src/network/events/relay.rs index cb5022e..ccd31ba 100644 --- a/examples/chat/src/network/events/relay.rs +++ b/examples/chat/src/network/events/relay.rs @@ -14,15 +14,18 @@ impl EventHandler for EventLoop { entry.reservation_state = RelayReservationState::Acquired; for (peer_id, entry) in self.rendezvous.iter() { - if entry.indetify_state.is_exchanged() { + if entry.identify_state.is_exchanged() { if let Err(err) = self.swarm.behaviour_mut().rendezvous.register( self.rendezvous_namespace.clone(), *peer_id, None, ) { - error!("Failed to register at rendezvous: {err}"); + error!(%err, "Failed to register at rendezvous"); } - debug!("Registered at rendezvous"); + debug!( + "Registered at rendezvous node {} on the namespace {}", + *peer_id, self.rendezvous_namespace + ); } } } diff --git a/examples/chat/src/network/events/rendezvous.rs b/examples/chat/src/network/events/rendezvous.rs index 06bb853..32cf0f7 100644 --- a/examples/chat/src/network/events/rendezvous.rs +++ b/examples/chat/src/network/events/rendezvous.rs @@ -55,7 +55,7 @@ impl EventHandler for EventLoop { let local_peer_id = *self.swarm.local_peer_id(); if peer == local_peer_id { if let Err(err) = self.swarm.behaviour_mut().rendezvous.register( - libp2p::rendezvous::Namespace::from_static("rendezvous"), + self.rendezvous_namespace.clone(), peer, None, ) {