use std::{ops::Deref, time::Instant};
use anyhow::Error;
use async_trait::async_trait;
use clap::Parser;
use minotari_app_utilities::utilities::UniPublicKey;
use tari_common_types::types::CompressedPublicKey;
use tari_comms_dht::envelope::NodeDestination;
use tokio::task;
use super::{CommandContext, HandleCommand};
#[derive(Debug, Parser)]
pub struct Args {
id: UniPublicKey,
}
#[async_trait]
impl HandleCommand<Args> for CommandContext {
async fn handle_command(&mut self, args: Args) -> Result<(), Error> {
self.discover_peer(Box::new(args.id.into())).await
}
}
impl CommandContext {
pub async fn discover_peer(&mut self, dest_pubkey: Box<CompressedPublicKey>) -> Result<(), Error> {
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(())
}
}