Skip to content

Commit

Permalink
validator: add contact-info query to admin port
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson authored and mergify[bot] committed Dec 23, 2021
1 parent d06c04d commit b93ab5d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
87 changes: 86 additions & 1 deletion validator/src/admin_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<ContactInfo> 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;
Expand Down Expand Up @@ -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<AdminRpcContactInfo>;
}

pub struct AdminRpcImpl;
Expand Down Expand Up @@ -167,6 +242,16 @@ impl AdminRpc for AdminRpcImpl {
))
}
}

fn contact_info(&self, meta: Self::Metadata) -> Result<AdminRpcContactInfo> {
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
Expand Down
32 changes: 32 additions & 0 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b93ab5d

Please sign in to comment.