From 6bc1bf7a6f0063cf1a7dd0d3a35c6d78ece78778 Mon Sep 17 00:00:00 2001 From: Sam Andreae Date: Sat, 22 Jun 2024 16:27:54 +0100 Subject: [PATCH] Even nicer log messages --- aquadoggo/src/network/service.rs | 72 +++++++++++++++----------------- aquadoggo/src/network/utils.rs | 5 +-- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/aquadoggo/src/network/service.rs b/aquadoggo/src/network/service.rs index 0ac93b150..a07b11689 100644 --- a/aquadoggo/src/network/service.rs +++ b/aquadoggo/src/network/service.rs @@ -171,9 +171,6 @@ impl EventLoop { pub async fn run(mut self) { let mut shutdown_request_received = self.shutdown_handler.is_requested(); - // Dial all known relay and direct peers. - self.attempt_dial_known_addresses().await; - loop { tokio::select! { event = self.swarm.next() => { @@ -286,37 +283,38 @@ impl EventLoop { .. } => { for Registration { record, .. } in registrations { - for address in record.addresses() { - let peer_id = record.peer_id(); - if peer_id != self.local_peer_id { - debug!("Peer discovered via rendezvous: {} {}", peer_id, address); + let peer_id = record.peer_id(); + let addresses = record.addresses(); + + debug!( + "Discovered {} addresses for peer {}", + addresses.len(), + peer_id + ); + + if peer_id != self.local_peer_id { + if self.swarm.is_connected(&peer_id) { + continue; + } - if self.swarm.is_connected(&peer_id) { - debug!("Already connected to peer {} not dialing", peer_id); - continue; - } + if let Some(relay_address) = self.relays.get(rendezvous_node) { + let peer_circuit_address = + relay_address.circuit_addr().with(Protocol::P2p(peer_id)); + + let opts = DialOpts::peer_id(peer_id) + .override_dial_concurrency_factor( + NonZeroU8::new(1).expect("Is nonzero u8"), + ) + .addresses(vec![peer_circuit_address]) + .build(); - if let Some(relay_address) = self.relays.get(rendezvous_node) { - debug!("Dialing peer {}", peer_id); - - let peer_circuit_address = - relay_address.circuit_addr().with(Protocol::P2p(peer_id)); - - let opts = DialOpts::peer_id(peer_id) - .override_dial_concurrency_factor( - NonZeroU8::new(1).expect("Is nonzero u8"), - ) - .addresses(vec![peer_circuit_address]) - .build(); - - match self.swarm.dial(opts) { - Ok(_) => (), - Err(err) => debug!("Error dialing peer: {:?}", err), - }; - } else { - debug!("Discovered peer from unknown relay node") + match self.swarm.dial(opts) { + Ok(_) => debug!("Dialed peer {}", peer_id), + Err(_) => (), }; - } + } else { + debug!("Discovered peer from unknown relay node") + }; } } } @@ -382,7 +380,7 @@ impl EventLoop { } // Add the relay to our known peers. - debug!("Relay identified: {peer_id} {addr}"); + debug!("Relay identified {peer_id} {addr}"); self.known_peers.insert(addr.clone(), *peer_id); // Also add it to our map of identified relays. @@ -395,7 +393,7 @@ impl EventLoop { listen_addrs, ) { // Add the direct node to our known peers. - debug!("Direct node identified: {peer_id} {addr}"); + debug!("Direct node identified {peer_id} {addr}"); self.known_peers.insert(addr, *peer_id); } } @@ -411,7 +409,7 @@ impl EventLoop { match relay.register(&mut self.swarm) { Ok(registered) => { if registered { - debug!("Registered on relay {} at {}", relay.peer_id, relay.addr) + debug!("Registration request sent to relay {}", relay.peer_id) } } Err(e) => debug!("Error registering on relay: {}", e), @@ -478,16 +476,12 @@ impl EventLoop { async fn handle_swarm_events(&mut self, event: SwarmEvent) { match event { SwarmEvent::ConnectionEstablished { - peer_id, - connection_id, endpoint, num_established, .. } => { debug!( - "Connection established with peer {}({}) at {}, total connections {}", - peer_id, - connection_id, + "Connected to {} (1/{})", endpoint.get_remote_address(), num_established ); diff --git a/aquadoggo/src/network/utils.rs b/aquadoggo/src/network/utils.rs index c9137120e..e19f0edff 100644 --- a/aquadoggo/src/network/utils.rs +++ b/aquadoggo/src/network/utils.rs @@ -75,8 +75,5 @@ pub fn dial_known_peer( // Dial the known peer. When dialing a peer by it's peer id this method will attempt a // new connections if we are already connected to the peer or we are already dialing // them. - match swarm.dial(opts) { - Ok(_) => (), - Err(err) => debug!("Error dialing node: {:?}", err), - }; + let _ = swarm.dial(opts); }