diff --git a/Cargo.lock b/Cargo.lock index cf87954f2..18b149617 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5970,6 +5970,8 @@ dependencies = [ "log 0.4.14", "num_cpus", "rand 0.7.3", + "serde", + "serde_json", "signal-hook", "solana-clap-utils", "solana-cli-config", diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 2a50bf5b7..f0a6e545d 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -25,6 +25,8 @@ jsonrpc-server-utils= "18.0.0" log = "0.4.14" num_cpus = "1.13.1" rand = "0.7.0" +serde = "1.0.132" +serde_json = "1.0.73" solana-clap-utils = { path = "../clap-utils", version = "=1.10.0" } solana-cli-config = { path = "../cli-config", version = "=1.10.0" } solana-client = { path = "../client", version = "=1.10.0" } diff --git a/validator/src/admin_rpc_service.rs b/validator/src/admin_rpc_service.rs index 6a3abc515..e330f2a81 100644 --- a/validator/src/admin_rpc_service.rs +++ b/validator/src/admin_rpc_service.rs @@ -5,15 +5,17 @@ use { jsonrpc_ipc_server::{RequestContext, ServerBuilder}, jsonrpc_server_utils::tokio, log::*, + serde::{Deserialize, Serialize}, solana_core::{ consensus::Tower, tower_storage::TowerStorage, validator::ValidatorStartProgress, }, - solana_gossip::cluster_info::ClusterInfo, + solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo}, solana_sdk::{ exit::Exit, signature::{read_keypair_file, Keypair, Signer}, }, std::{ + fmt::{self, Display}, net::SocketAddr, path::{Path, PathBuf}, sync::{Arc, RwLock}, @@ -34,6 +36,76 @@ pub struct AdminRpcRequestMetadata { } impl Metadata for AdminRpcRequestMetadata {} +#[derive(Debug, Deserialize, Serialize)] +pub struct AdminRpcContactInfo { + pub id: String, + pub gossip: SocketAddr, + pub tvu: SocketAddr, + pub tvu_forwards: SocketAddr, + pub repair: SocketAddr, + pub tpu: SocketAddr, + pub tpu_forwards: SocketAddr, + pub tpu_vote: SocketAddr, + pub rpc: SocketAddr, + pub rpc_pubsub: SocketAddr, + pub serve_repair: SocketAddr, + pub last_updated_timestamp: u64, + pub shred_version: u16, +} + +impl From for AdminRpcContactInfo { + fn from(contact_info: ContactInfo) -> Self { + let ContactInfo { + id, + gossip, + tvu, + tvu_forwards, + repair, + tpu, + tpu_forwards, + tpu_vote, + rpc, + rpc_pubsub, + serve_repair, + wallclock, + shred_version, + } = contact_info; + Self { + id: id.to_string(), + last_updated_timestamp: wallclock, + gossip, + tvu, + tvu_forwards, + repair, + tpu, + tpu_forwards, + tpu_vote, + rpc, + rpc_pubsub, + serve_repair, + shred_version, + } + } +} + +impl Display for AdminRpcContactInfo { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + writeln!(f, "Identity: {}", self.id)?; + writeln!(f, "Gossip: {}", self.gossip)?; + writeln!(f, "TVU: {}", self.tvu)?; + writeln!(f, "TVU Forwards: {}", self.tvu_forwards)?; + writeln!(f, "Repair: {}", self.repair)?; + writeln!(f, "TPU: {}", self.tpu)?; + writeln!(f, "TPU Forwards: {}", self.tpu_forwards)?; + writeln!(f, "TPU Votes: {}", self.tpu_vote)?; + writeln!(f, "RPC: {}", self.rpc)?; + writeln!(f, "RPC Pubsub: {}", self.rpc_pubsub)?; + writeln!(f, "Serve Repair: {}", self.serve_repair)?; + writeln!(f, "Last Updated Timestamp: {}", self.last_updated_timestamp)?; + writeln!(f, "Shred Version: {}", self.shred_version) + } +} + #[rpc] pub trait AdminRpc { type Metadata; @@ -61,6 +133,9 @@ pub trait AdminRpc { #[rpc(meta, name = "setIdentity")] fn set_identity(&self, meta: Self::Metadata, keypair_file: String) -> Result<()>; + + #[rpc(meta, name = "contactInfo")] + fn contact_info(&self, meta: Self::Metadata) -> Result; } pub struct AdminRpcImpl; @@ -167,6 +242,16 @@ impl AdminRpc for AdminRpcImpl { )) } } + + fn contact_info(&self, meta: Self::Metadata) -> Result { + if let Some(cluster_info) = meta.cluster_info.read().unwrap().as_ref() { + Ok(cluster_info.my_contact_info().into()) + } else { + Err(jsonrpc_core::error::Error::invalid_params( + "Retry once validator start up is complete", + )) + } + } } // Start the Admin RPC interface diff --git a/validator/src/main.rs b/validator/src/main.rs index b7a5e7618..d2da166d7 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -1682,6 +1682,18 @@ pub fn main() { currently running validator instance") ) ) + .subcommand( + SubCommand::with_name("contact-info") + .about("Display the validator's contact info") + .arg( + Arg::with_name("output") + .long("output") + .takes_value(true) + .value_name("MODE") + .possible_values(&["json", "json-compact"]) + .help("Output display mode") + ) + ) .subcommand( SubCommand::with_name("init") .about("Initialize the ledger directory then exit") @@ -1809,6 +1821,26 @@ pub fn main() { _ => unreachable!(), } } + ("contact-info", Some(subcommand_matches)) => { + let output_mode = subcommand_matches.value_of("output"); + let admin_client = admin_rpc_service::connect(&ledger_path); + let contact_info = admin_rpc_service::runtime() + .block_on(async move { admin_client.await?.contact_info().await }) + .unwrap_or_else(|err| { + eprintln!("Contact info query failed: {}", err); + exit(1); + }); + if let Some(mode) = output_mode { + match mode { + "json" => println!("{}", serde_json::to_string_pretty(&contact_info).unwrap()), + "json-compact" => print!("{}", serde_json::to_string(&contact_info).unwrap()), + _ => unreachable!(), + } + } else { + print!("{}", contact_info); + } + return; + } ("init", _) => Operation::Initialize, ("exit", Some(subcommand_matches)) => { let min_idle_time = value_t_or_exit!(subcommand_matches, "min_idle_time", usize);