Skip to content

Commit

Permalink
fix(base-node): make dial-peer and discover-peer run in their own task
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jul 7, 2022
1 parent 5b8c8ee commit 5bd5ef5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
20 changes: 15 additions & 5 deletions applications/tari_base_node/src/commands/command/dial_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use async_trait::async_trait;
use clap::Parser;
use tari_app_utilities::utilities::UniNodeId;
use tari_comms::peer_manager::NodeId;
use tokio::task;

use super::{CommandContext, HandleCommand};

Expand All @@ -47,12 +48,21 @@ impl HandleCommand<Args> for CommandContext {
impl CommandContext {
/// Function to process the dial-peer command
pub async fn dial_peer(&self, dest_node_id: NodeId) -> Result<(), Error> {
let start = Instant::now();
println!("☎️ Dialing peer...");
let mut connectivity = self.connectivity.clone();
task::spawn(async move {
let start = Instant::now();
println!("☎️ Dialing peer...");

let connection = self.connectivity.dial_peer(dest_node_id).await?;
println!("⚡️ Peer connected in {}ms!", start.elapsed().as_millis());
println!("Connection: {}", connection);
match connectivity.dial_peer(dest_node_id).await {
Ok(connection) => {
println!("⚡️ Peer connected in {}ms!", start.elapsed().as_millis());
println!("Connection: {}", connection);
},
Err(err) => {
println!("☠️ {}", err);
},
}
});
Ok(())
}
}
28 changes: 19 additions & 9 deletions applications/tari_base_node/src/commands/command/discover_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use clap::Parser;
use tari_app_utilities::utilities::UniPublicKey;
use tari_comms_dht::envelope::NodeDestination;
use tari_crypto::ristretto::RistrettoPublicKey;
use tokio::task;

use super::{CommandContext, HandleCommand};

Expand All @@ -48,15 +49,24 @@ impl HandleCommand<Args> for CommandContext {
impl CommandContext {
/// Function to process the discover-peer command
pub async fn discover_peer(&mut self, dest_pubkey: Box<RistrettoPublicKey>) -> Result<(), Error> {
let start = Instant::now();
println!("🌎 Peer discovery started.");
let peer = self
.discovery_service
.discover_peer(dest_pubkey.deref().clone(), NodeDestination::PublicKey(dest_pubkey))
.await?;
println!("⚡️ Discovery succeeded in {}ms!", start.elapsed().as_millis());
println!("This peer was found:");
println!("{}", peer);
let mut discovery_service = self.discovery_service.clone();
task::spawn(async move {
let start = Instant::now();
println!("🌎 Peer discovery started.");
match discovery_service
.discover_peer(dest_pubkey.deref().clone(), NodeDestination::PublicKey(dest_pubkey))
.await
{
Ok(peer) => {
println!("⚡️ Discovery succeeded in {}ms!", start.elapsed().as_millis());
println!("This peer was found:");
println!("{}", peer);
},
Err(err) => {
println!("☠️ {}", err);
},
}
});
Ok(())
}
}

0 comments on commit 5bd5ef5

Please sign in to comment.