Skip to content

Commit

Permalink
chore: fix typos, remove unnecessary clones
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 21, 2024
1 parent 6ebdebd commit 8f8a64f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
29 changes: 16 additions & 13 deletions examples/chat/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,9 +38,9 @@ pub async fn run(
rendezvous_addresses: Vec<Multiaddr>,
) -> eyre::Result<(NetworkClient, mpsc::Receiver<types::NetworkEvent>)> {
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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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::<std::collections::HashSet<_>>()
.into_iter()
{
info!("Dialing address: {:?}", addr);
if let Err(err) = client.dial(addr).await {
error!("Failed to dial rendezvous address: {}", err);
};
Expand Down Expand Up @@ -172,15 +175,15 @@ pub(crate) struct EventLoop {
#[derive(Debug)]
pub(crate) struct RelayEntry {
address: Multiaddr,
indetify_state: IdentifyState,
identify_state: IdentifyState,
reservation_state: RelayReservationState,
}

impl RelayEntry {
pub(crate) fn new(address: Multiaddr) -> Self {
Self {
address,
indetify_state: Default::default(),
identify_state: Default::default(),
reservation_state: Default::default(),
}
}
Expand All @@ -190,15 +193,15 @@ impl RelayEntry {
pub(crate) struct RendezvousEntry {
address: Multiaddr,
cookie: Option<rendezvous::Cookie>,
indetify_state: IdentifyState,
identify_state: IdentifyState,
}

impl RendezvousEntry {
pub(crate) fn new(address: Multiaddr) -> Self {
Self {
address,
cookie: Default::default(),
indetify_state: Default::default(),
identify_state: Default::default(),
}
}
}
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -399,7 +402,7 @@ pub(crate) struct MeshPeerInfo {
peers: Vec<PeerId>,
}

pub(crate) fn peak_peer_id(address: &Multiaddr) -> eyre::Result<PeerId> {
pub(crate) fn peek_peer_id(address: &Multiaddr) -> eyre::Result<PeerId> {
match address.iter().last() {
Some(proto) => match proto {
multiaddr::Protocol::P2p(peer_id) => Ok(peer_id),
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/src/network/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions examples/chat/src/network/events/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ impl EventHandler<identify::Event> 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
Expand All @@ -27,14 +27,14 @@ impl EventHandler<identify::Event> 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
Expand All @@ -48,7 +48,7 @@ impl EventHandler<identify::Event> for EventLoop {
}

if let Some(entry) = self.rendezvous.get_mut(&peer_id) {
entry.indetify_state.sent = true;
entry.identify_state.sent = true;
}
}
_ => {}
Expand Down
9 changes: 6 additions & 3 deletions examples/chat/src/network/events/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ impl EventHandler<relay::client::Event> 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
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/src/network/events/rendezvous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl EventHandler<rendezvous::client::Event> 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,
) {
Expand Down

0 comments on commit 8f8a64f

Please sign in to comment.