debash: Add solana-gossip get-rpc-url command to avoid hard coding (#5513)
				
					
				
			This commit is contained in:
		| @@ -107,7 +107,7 @@ pub fn discover( | |||||||
|     ); |     ); | ||||||
|     Err(std::io::Error::new( |     Err(std::io::Error::new( | ||||||
|         std::io::ErrorKind::Other, |         std::io::ErrorKind::Other, | ||||||
|         "Failed to converge", |         "Discover failed", | ||||||
|     )) |     )) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,7 +3,10 @@ | |||||||
| #[macro_use] | #[macro_use] | ||||||
| extern crate solana; | extern crate solana; | ||||||
|  |  | ||||||
| use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg, SubCommand}; | use clap::{ | ||||||
|  |     crate_description, crate_name, crate_version, value_t_or_exit, App, AppSettings, Arg, | ||||||
|  |     SubCommand, | ||||||
|  | }; | ||||||
| use solana::contact_info::ContactInfo; | use solana::contact_info::ContactInfo; | ||||||
| use solana::gossip_service::discover; | use solana::gossip_service::discover; | ||||||
| use solana_client::rpc_client::RpcClient; | use solana_client::rpc_client::RpcClient; | ||||||
| @@ -38,6 +41,19 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                 .global(true) |                 .global(true) | ||||||
|                 .help("Rendezvous with the cluster at this entry point"), |                 .help("Rendezvous with the cluster at this entry point"), | ||||||
|         ) |         ) | ||||||
|  |         .subcommand( | ||||||
|  |             SubCommand::with_name("get-rpc-url") | ||||||
|  |                 .about("Get an RPC URL for the cluster") | ||||||
|  |                 .arg( | ||||||
|  |                     Arg::with_name("timeout") | ||||||
|  |                         .long("timeout") | ||||||
|  |                         .value_name("SECONDS") | ||||||
|  |                         .takes_value(true) | ||||||
|  |                         .default_value("5") | ||||||
|  |                         .help("Timeout in seconds"), | ||||||
|  |                 ) | ||||||
|  |                 .setting(AppSettings::DisableVersion), | ||||||
|  |         ) | ||||||
|         .subcommand( |         .subcommand( | ||||||
|             SubCommand::with_name("spy") |             SubCommand::with_name("spy") | ||||||
|                 .about("Monitor the gossip entrypoint") |                 .about("Monitor the gossip entrypoint") | ||||||
| @@ -49,7 +65,7 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                         .value_name("NUM") |                         .value_name("NUM") | ||||||
|                         .takes_value(true) |                         .takes_value(true) | ||||||
|                         .conflicts_with("num_nodes_exactly") |                         .conflicts_with("num_nodes_exactly") | ||||||
|                         .help("Wait for at least NUM nodes to converge"), |                         .help("Wait for at least NUM nodes to be visible"), | ||||||
|                 ) |                 ) | ||||||
|                 .arg( |                 .arg( | ||||||
|                     Arg::with_name("num_nodes_exactly") |                     Arg::with_name("num_nodes_exactly") | ||||||
| @@ -57,7 +73,7 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                         .long("num-nodes-exactly") |                         .long("num-nodes-exactly") | ||||||
|                         .value_name("NUM") |                         .value_name("NUM") | ||||||
|                         .takes_value(true) |                         .takes_value(true) | ||||||
|                         .help("Wait for exactly NUM nodes to converge"), |                         .help("Wait for exactly NUM nodes to be visible"), | ||||||
|                 ) |                 ) | ||||||
|                 .arg( |                 .arg( | ||||||
|                     Arg::with_name("node_pubkey") |                     Arg::with_name("node_pubkey") | ||||||
| @@ -71,11 +87,9 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                 .arg( |                 .arg( | ||||||
|                     Arg::with_name("timeout") |                     Arg::with_name("timeout") | ||||||
|                         .long("timeout") |                         .long("timeout") | ||||||
|                         .value_name("SECS") |                         .value_name("SECONDS") | ||||||
|                         .takes_value(true) |                         .takes_value(true) | ||||||
|                         .help( |                         .help("Maximum time to wait in seconds [default: wait forever]"), | ||||||
|                             "Maximum time to wait for cluster to converge [default: wait forever]", |  | ||||||
|                         ), |  | ||||||
|                 ), |                 ), | ||||||
|         ) |         ) | ||||||
|         .subcommand( |         .subcommand( | ||||||
| @@ -99,6 +113,18 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|             exit(1) |             exit(1) | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     let gossip_addr = { | ||||||
|  |         let mut addr = socketaddr_any!(); | ||||||
|  |         addr.set_ip( | ||||||
|  |             solana_netutil::get_public_ip_addr(&entrypoint_addr).unwrap_or_else(|err| { | ||||||
|  |                 eprintln!("failed to contact {}: {}", entrypoint_addr, err); | ||||||
|  |                 exit(1) | ||||||
|  |             }), | ||||||
|  |         ); | ||||||
|  |         Some(addr) | ||||||
|  |     }; | ||||||
|  |  | ||||||
|     match matches.subcommand() { |     match matches.subcommand() { | ||||||
|         ("spy", Some(matches)) => { |         ("spy", Some(matches)) => { | ||||||
|             let num_nodes_exactly = matches |             let num_nodes_exactly = matches | ||||||
| @@ -115,17 +141,6 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                 .value_of("node_pubkey") |                 .value_of("node_pubkey") | ||||||
|                 .map(|pubkey_str| pubkey_str.parse::<Pubkey>().unwrap()); |                 .map(|pubkey_str| pubkey_str.parse::<Pubkey>().unwrap()); | ||||||
|  |  | ||||||
|             let gossip_addr = { |  | ||||||
|                 let mut addr = socketaddr_any!(); |  | ||||||
|                 addr.set_ip( |  | ||||||
|                     solana_netutil::get_public_ip_addr(&entrypoint_addr).unwrap_or_else(|err| { |  | ||||||
|                         eprintln!("failed to contact {}: {}", entrypoint_addr, err); |  | ||||||
|                         exit(1) |  | ||||||
|                     }), |  | ||||||
|                 ); |  | ||||||
|                 Some(addr) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             let (nodes, _replicators) = discover( |             let (nodes, _replicators) = discover( | ||||||
|                 &entrypoint_addr, |                 &entrypoint_addr, | ||||||
|                 num_nodes, |                 num_nodes, | ||||||
| @@ -161,13 +176,42 @@ fn main() -> Result<(), Box<dyn error::Error>> { | |||||||
|                 ); |                 ); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         ("get-rpc-url", Some(matches)) => { | ||||||
|  |             let timeout = value_t_or_exit!(matches, "timeout", u64); | ||||||
|  |             let (nodes, _replicators) = discover( | ||||||
|  |                 &entrypoint_addr, | ||||||
|  |                 Some(1), | ||||||
|  |                 Some(timeout), | ||||||
|  |                 None, | ||||||
|  |                 gossip_addr.as_ref(), | ||||||
|  |             )?; | ||||||
|  |  | ||||||
|  |             let rpc_addr = nodes | ||||||
|  |                 .iter() | ||||||
|  |                 .filter_map(ContactInfo::valid_client_facing_addr) | ||||||
|  |                 .map(|addrs| addrs.0) | ||||||
|  |                 .find(|rpc_addr| rpc_addr.ip() == entrypoint_addr.ip()); | ||||||
|  |  | ||||||
|  |             if rpc_addr.is_none() { | ||||||
|  |                 eprintln!("No RPC URL found"); | ||||||
|  |                 exit(1); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             println!("http://{}", rpc_addr.unwrap()); | ||||||
|  |         } | ||||||
|         ("stop", Some(matches)) => { |         ("stop", Some(matches)) => { | ||||||
|             let pubkey = matches |             let pubkey = matches | ||||||
|                 .value_of("node_pubkey") |                 .value_of("node_pubkey") | ||||||
|                 .unwrap() |                 .unwrap() | ||||||
|                 .parse::<Pubkey>() |                 .parse::<Pubkey>() | ||||||
|                 .unwrap(); |                 .unwrap(); | ||||||
|             let (nodes, _replicators) = discover(&entrypoint_addr, None, None, Some(pubkey), None)?; |             let (nodes, _replicators) = discover( | ||||||
|  |                 &entrypoint_addr, | ||||||
|  |                 None, | ||||||
|  |                 None, | ||||||
|  |                 Some(pubkey), | ||||||
|  |                 gossip_addr.as_ref(), | ||||||
|  |             )?; | ||||||
|             let node = nodes.iter().find(|x| x.id == pubkey).unwrap(); |             let node = nodes.iter().find(|x| x.id == pubkey).unwrap(); | ||||||
|  |  | ||||||
|             if !ContactInfo::is_valid_address(&node.rpc) { |             if !ContactInfo::is_valid_address(&node.rpc) { | ||||||
|   | |||||||
| @@ -56,7 +56,7 @@ done | |||||||
| : "${storage_keypair:="$SOLANA_ROOT"/farf/replicator-storage-keypair"$label".json}" | : "${storage_keypair:="$SOLANA_ROOT"/farf/replicator-storage-keypair"$label".json}" | ||||||
| ledger="$SOLANA_ROOT"/farf/replicator-ledger"$label" | ledger="$SOLANA_ROOT"/farf/replicator-ledger"$label" | ||||||
|  |  | ||||||
| rpc_url=$("$here"/rpc-url.sh "$entrypoint") | rpc_url=$($solana_gossip get-rpc-url --entrypoint "$entrypoint") | ||||||
|  |  | ||||||
| if [[ ! -r $identity_keypair ]]; then | if [[ ! -r $identity_keypair ]]; then | ||||||
|   $solana_keygen new -o "$identity_keypair" |   $solana_keygen new -o "$identity_keypair" | ||||||
|   | |||||||
| @@ -1,14 +0,0 @@ | |||||||
| #!/usr/bin/env bash |  | ||||||
| # |  | ||||||
| # Given a gossip entrypoint derive the entrypoint's RPC address |  | ||||||
| # |  | ||||||
|  |  | ||||||
| entrypoint_address=$1 |  | ||||||
| if [[ -z $entrypoint_address ]]; then |  | ||||||
|   echo "Error: entrypoint address not specified" >&2 |  | ||||||
|   exit 1 |  | ||||||
| fi |  | ||||||
|  |  | ||||||
| # TODO: Rather than hard coding, add a `solana-gossip rpc-address` command that |  | ||||||
| #       actually asks the entrypoint itself for its RPC address |  | ||||||
| echo "http://${entrypoint_address%:*}:8899" |  | ||||||
| @@ -175,7 +175,7 @@ else | |||||||
|     gossip_entrypoint="$entrypoint_hostname":8001 |     gossip_entrypoint="$entrypoint_hostname":8001 | ||||||
|   fi |   fi | ||||||
| fi | fi | ||||||
| rpc_url=$("$here"/rpc-url.sh "$gossip_entrypoint") |  | ||||||
| drone_address="${gossip_entrypoint%:*}":9900 | drone_address="${gossip_entrypoint%:*}":9900 | ||||||
|  |  | ||||||
| : "${identity_keypair_path:=$ledger_dir/identity-keypair.json}" | : "${identity_keypair_path:=$ledger_dir/identity-keypair.json}" | ||||||
| @@ -291,6 +291,7 @@ setup_validator_accounts() { | |||||||
| } | } | ||||||
|  |  | ||||||
| while true; do | while true; do | ||||||
|  |   rpc_url=$($solana_gossip get-rpc-url --entrypoint "$gossip_entrypoint") | ||||||
|   if new_genesis_block; then |   if new_genesis_block; then | ||||||
|     # If the genesis block has changed remove the now stale ledger and start all |     # If the genesis block has changed remove the now stale ledger and start all | ||||||
|     # over again |     # over again | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user