From f910cce13aa6ba3af021253bd922baddd43e885f Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Thu, 7 Jul 2022 11:06:14 +0200 Subject: [PATCH] fix(base-node): minor fixups for hex/type parsing and long running commands (#4281) Description --- 1. [fix(base-node): make dial-peer and discover-peer run in their own task](https://github.com/tari-project/tari/commit/5bd5ef5caab4355783ff11991ce17c261ef9e6c7) 1. [fix(base-node): when parsing hex or type commands, do string parsing first](https://github.com/tari-project/tari/commit/1d3dde3130d8e00749c48e173a33d0ab9dd25c54) 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 --- .../src/commands/command/dial_peer.rs | 20 +++++++++---- .../src/commands/command/discover_peer.rs | 28 +++++++++++++------ .../src/commands/command/mod.rs | 17 ++++++----- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/applications/tari_base_node/src/commands/command/dial_peer.rs b/applications/tari_base_node/src/commands/command/dial_peer.rs index 9432480e4f..d7dcbd8815 100644 --- a/applications/tari_base_node/src/commands/command/dial_peer.rs +++ b/applications/tari_base_node/src/commands/command/dial_peer.rs @@ -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}; @@ -47,12 +48,21 @@ impl HandleCommand 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(()) } } diff --git a/applications/tari_base_node/src/commands/command/discover_peer.rs b/applications/tari_base_node/src/commands/command/discover_peer.rs index ee6a9c3611..5613fd622e 100644 --- a/applications/tari_base_node/src/commands/command/discover_peer.rs +++ b/applications/tari_base_node/src/commands/command/discover_peer.rs @@ -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}; @@ -48,15 +49,24 @@ impl HandleCommand for CommandContext { impl CommandContext { /// Function to process the discover-peer command pub async fn discover_peer(&mut self, dest_pubkey: Box) -> 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(()) } } diff --git a/applications/tari_base_node/src/commands/command/mod.rs b/applications/tari_base_node/src/commands/command/mod.rs index 57defe0922..927bb55cfd 100644 --- a/applications/tari_base_node/src/commands/command/mod.rs +++ b/applications/tari_base_node/src/commands/command/mod.rs @@ -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}; @@ -285,17 +285,20 @@ pub enum TypeOrHex { } impl FromStr for TypeOrHex -where - T: FromStr, - Error: From, +where T: FromStr { type Err = Error; fn from_str(s: &str) -> Result { - 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::() + ) + }) } } }