Skip to content

Commit

Permalink
fix(base-node): minor fixups for hex/type parsing and long running co…
Browse files Browse the repository at this point in the history
…mmands (#4281)

Description
---
1. [fix(base-node): make dial-peer and discover-peer run in their own task](5bd5ef5)
1. [fix(base-node): when parsing hex or type commands, do string parsing first](1d3dde3) 

Motivation and Context
---
1. Some commands can run for a long time, while they are running they cannot be interrupted and no other commands can be issued. This change makes dial-peer and discover-peer run on their own task so that they do not block other commands.
2. get-block 1234 would be interpreted as hex instead of as a block height. This PR fixes that.

How Has This Been Tested?
---
Manually
  • Loading branch information
sdbondi authored Jul 7, 2022
1 parent 08aa4a6 commit f910cce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 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 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(())
}
}
17 changes: 10 additions & 7 deletions applications/tari_base_node/src/commands/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use std::{
time::{Duration, Instant},
};

use anyhow::Error;
use anyhow::{anyhow, Error};
use async_trait::async_trait;
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
use strum::{EnumVariantNames, VariantNames};
Expand Down Expand Up @@ -285,17 +285,20 @@ pub enum TypeOrHex<T> {
}

impl<T> FromStr for TypeOrHex<T>
where
T: FromStr,
Error: From<T::Err>,
where T: FromStr
{
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(hex) = FromHex::from_str(s) {
Ok(Self::Hex(hex))
if let Ok(t) = T::from_str(s) {
Ok(Self::Type(t))
} else {
T::from_str(s).map(Self::Type).map_err(Error::from)
FromHex::from_str(s).map(Self::Hex).map_err(|_| {
anyhow!(
"Argument was not a valid string for {} or hex value",
std::any::type_name::<T>()
)
})
}
}
}

0 comments on commit f910cce

Please sign in to comment.