From 53c85de43a7cf618c3c81200b97e29330ef9cfc0 Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Fri, 2 Feb 2018 19:37:35 -0500 Subject: [PATCH] List connected peers (#680) * Removed unused crates * Add listconnectedpeers in grin client --- Cargo.toml | 9 ++++----- src/bin/client.rs | 33 +++++++++++++++++++++++++++++++-- src/bin/grin.rs | 8 +++++++- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 251069d7..86de1ad7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,18 +13,17 @@ path = "src/bin/grin.rs" [dependencies] grin_api = { path = "./api" } -grin_wallet = { path = "./wallet" } -grin_keychain = { path = "./keychain" } -grin_grin = { path = "./grin" } grin_config = { path = "./config" } grin_core = { path = "./core" } -grin_pow = { path = "./pow"} +grin_grin = { path = "./grin" } +grin_keychain = { path = "./keychain" } +grin_p2p = { path = "./p2p"} grin_util = { path = "./util"} +grin_wallet = { path = "./wallet" } blake2-rfc = "~0.2.17" clap = "^2.23.3" daemonize = "^0.2.3" serde = "~1.0.8" -serde_derive = "~1.0.8" serde_json = "~1.0.7" slog = { version = "^2.0.12", features = ["max_level_trace", "release_max_level_trace"] } term = "~0.4.6" diff --git a/src/bin/client.rs b/src/bin/client.rs index d804d8c7..118431a1 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -15,7 +15,9 @@ extern crate term; use std::net::SocketAddr; + use api; +use p2p; use grin::ServerConfig; pub fn show_status(config: &ServerConfig) { @@ -35,7 +37,8 @@ pub fn show_status(config: &ServerConfig) { writeln!(e, "Chain height: {}", status.tip.height).unwrap(); writeln!(e, "Last block hash: {}", status.tip.last_block_pushed).unwrap(); writeln!(e, "Previous block hash: {}", status.tip.prev_block_to_last).unwrap(); - writeln!(e, "Total difficulty: {}", status.tip.total_difficulty).unwrap() + writeln!(e, "Total difficulty: {}", status.tip.total_difficulty).unwrap(); + } Err(_) => writeln!( e, @@ -43,7 +46,7 @@ pub fn show_status(config: &ServerConfig) { ).unwrap(), }; e.reset().unwrap(); - println!(); + println!() } pub fn ban_peer(config: &ServerConfig, peer_addr: &SocketAddr) { @@ -73,6 +76,32 @@ pub fn unban_peer(config: &ServerConfig, peer_addr: &SocketAddr) { Ok(_) => writeln!(e, "Successfully unbanned peer {}", peer_addr).unwrap(), Err(_) => writeln!(e, "Failed to unban peer {}", peer_addr).unwrap(), }; + e.reset().unwrap(); +} + +pub fn list_connected_peers(config: &ServerConfig) { + let mut e = term::stdout().unwrap(); + let url = format!( + "http://{}/v1/peers/connected", + config.api_http_addr + ); + match api::client::get::>(url.as_str()).map_err(|e| Error::API(e)) { + Ok(connected_peers) => { + let mut index = 0; + for connected_peer in connected_peers { + writeln!(e, "Peer {}:", index).unwrap(); + writeln!(e, "Capabilities: {:?}", connected_peer.capabilities).unwrap(); + writeln!(e, "User agent: {}", connected_peer.user_agent).unwrap(); + writeln!(e, "Version: {}", connected_peer.version).unwrap(); + writeln!(e, "Peer address: {}", connected_peer.addr).unwrap(); + writeln!(e, "Total difficulty: {}", connected_peer.total_difficulty).unwrap(); + println!(); + index = index + 1; + } + }, + Err(_) => writeln!(e, "Failed to get connected peers").unwrap(), + }; + e.reset().unwrap(); } fn get_status_from_node(config: &ServerConfig) -> Result { diff --git a/src/bin/grin.rs b/src/bin/grin.rs index d00944a4..df6ea224 100644 --- a/src/bin/grin.rs +++ b/src/bin/grin.rs @@ -27,6 +27,7 @@ extern crate grin_config as config; extern crate grin_core as core; extern crate grin_grin as grin; extern crate grin_keychain as keychain; +extern crate grin_p2p as p2p; extern crate grin_util as util; extern crate grin_wallet as wallet; @@ -151,7 +152,9 @@ fn main() { .subcommand(SubCommand::with_name("client") .about("Communicates with the Grin server") .subcommand(SubCommand::with_name("status") - .about("current status of the Grin chain")) + .about("Current status of the Grin chain")) + .subcommand(SubCommand::with_name("listconnectedpeers") + .about("Print a list of currently connected peers")) .subcommand(SubCommand::with_name("ban") .about("Ban peer") .arg(Arg::with_name("peer") @@ -385,6 +388,9 @@ fn client_command(client_args: &ArgMatches, global_config: GlobalConfig) { ("status", Some(_)) => { client::show_status(&server_config); } + ("listconnectedpeers", Some(_)) => { + client::list_connected_peers(&server_config); + } ("ban", Some(peer_args)) => { if let Some(peer) = peer_args.value_of("peer") { if let Ok(addr) = peer.parse() {