Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(base-node): minor fixups for hex/type parsing and long running commands #4281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>()
)
})
}
}
}