From 1ab50985762e0e104b9cdc838416ab629eaf79c5 Mon Sep 17 00:00:00 2001 From: sakridge Date: Wed, 1 May 2019 17:14:01 -0700 Subject: [PATCH] Move get_clients into gossip_service (#4109) --- bench-exchange/src/bench.rs | 24 +----------------------- bench-exchange/src/main.rs | 4 ++-- bench-tps/src/main.rs | 22 +++------------------- core/src/contact_info.rs | 19 +++++++++++++++++++ core/src/gossip_service.rs | 10 ++++++++++ 5 files changed, 35 insertions(+), 44 deletions(-) diff --git a/bench-exchange/src/bench.rs b/bench-exchange/src/bench.rs index a686d97c0..596b65aac 100644 --- a/bench-exchange/src/bench.rs +++ b/bench-exchange/src/bench.rs @@ -5,12 +5,8 @@ use itertools::izip; use log::*; use rand::{thread_rng, Rng}; use rayon::prelude::*; -use solana::cluster_info::FULLNODE_PORT_RANGE; -use solana::contact_info::ContactInfo; use solana::gen_keys::GenKeys; use solana_client::perf_utils::{sample_txs, SampleStats}; -use solana_client::thin_client::create_client; -use solana_client::thin_client::ThinClient; use solana_drone::drone::request_airdrop_transaction; use solana_exchange_api::exchange_instruction; use solana_exchange_api::exchange_state::*; @@ -907,29 +903,11 @@ pub fn airdrop_lamports(client: &Client, drone_addr: &SocketAddr, id: &Keypair, } } -pub fn get_clients(nodes: &[ContactInfo]) -> Vec { - nodes - .iter() - .filter_map(|node| { - let cluster_entrypoint = node; - let cluster_addrs = cluster_entrypoint.client_facing_addr(); - if ContactInfo::is_valid_address(&cluster_addrs.0) - && ContactInfo::is_valid_address(&cluster_addrs.1) - { - let client = create_client(cluster_addrs, FULLNODE_PORT_RANGE); - Some(client) - } else { - None - } - }) - .collect() -} - #[cfg(test)] mod tests { use super::*; use solana::fullnode::FullnodeConfig; - use solana::gossip_service::discover_nodes; + use solana::gossip_service::{discover_nodes, get_clients}; use solana::local_cluster::{ClusterConfig, LocalCluster}; use solana_drone::drone::run_local_drone; use solana_exchange_api::exchange_processor::process_instruction; diff --git a/bench-exchange/src/main.rs b/bench-exchange/src/main.rs index 35113065c..ec6f5d4a1 100644 --- a/bench-exchange/src/main.rs +++ b/bench-exchange/src/main.rs @@ -2,9 +2,9 @@ pub mod bench; mod cli; pub mod order_book; -use crate::bench::{airdrop_lamports, do_bench_exchange, get_clients, Config}; +use crate::bench::{airdrop_lamports, do_bench_exchange, Config}; use log::*; -use solana::gossip_service::discover_nodes; +use solana::gossip_service::{discover_nodes, get_clients}; use solana_sdk::signature::KeypairUtil; fn main() { diff --git a/bench-tps/src/main.rs b/bench-tps/src/main.rs index a8479ccd0..2d33a3331 100644 --- a/bench-tps/src/main.rs +++ b/bench-tps/src/main.rs @@ -2,10 +2,7 @@ mod bench; mod cli; use crate::bench::{do_bench_tps, generate_and_fund_keypairs, Config, NUM_LAMPORTS_PER_ACCOUNT}; -use solana::cluster_info::FULLNODE_PORT_RANGE; -use solana::contact_info::ContactInfo; -use solana::gossip_service::discover_nodes; -use solana_client::thin_client::create_client; +use solana::gossip_service::{discover_nodes, get_clients}; use std::process::exit; fn main() { @@ -39,21 +36,8 @@ fn main() { ); exit(1); } - let clients: Vec<_> = nodes - .iter() - .filter_map(|node| { - let cluster_entrypoint = node.clone(); - let cluster_addrs = cluster_entrypoint.client_facing_addr(); - if ContactInfo::is_valid_address(&cluster_addrs.0) - && ContactInfo::is_valid_address(&cluster_addrs.1) - { - let client = create_client(cluster_addrs, FULLNODE_PORT_RANGE); - Some(client) - } else { - None - } - }) - .collect(); + + let clients = get_clients(&nodes); let (keypairs, keypair_balance) = generate_and_fund_keypairs( &clients[0], diff --git a/core/src/contact_info.rs b/core/src/contact_info.rs index 7dd070071..e56570b6a 100644 --- a/core/src/contact_info.rs +++ b/core/src/contact_info.rs @@ -210,6 +210,14 @@ impl ContactInfo { pub fn client_facing_addr(&self) -> (SocketAddr, SocketAddr) { (self.rpc, self.tpu) } + + pub fn valid_client_facing_addr(&self) -> Option<(SocketAddr, SocketAddr)> { + if ContactInfo::is_valid_address(&self.rpc) && ContactInfo::is_valid_address(&self.tpu) { + Some((self.rpc, self.tpu)) + } else { + None + } + } } impl Signable for ContactInfo { @@ -272,6 +280,7 @@ mod tests { assert!(ContactInfo::is_valid_address(&loopback)); // assert!(!ContactInfo::is_valid_ip_internal(loopback.ip(), false)); } + #[test] fn test_default() { let ci = ContactInfo::default(); @@ -333,4 +342,14 @@ mod tests { assert_eq!(d1.rpc, socketaddr!("127.0.0.1:8899")); assert_eq!(d1.rpc_pubsub, socketaddr!("127.0.0.1:8900")); } + + #[test] + fn test_valid_client_facing() { + let mut ci = ContactInfo::default(); + assert_eq!(ci.valid_client_facing_addr(), None); + ci.tpu = socketaddr!("127.0.0.1:123"); + assert_eq!(ci.valid_client_facing_addr(), None); + ci.rpc = socketaddr!("127.0.0.1:234"); + assert!(ci.valid_client_facing_addr().is_some()); + } } diff --git a/core/src/gossip_service.rs b/core/src/gossip_service.rs index 9ea149882..1fe8490fc 100644 --- a/core/src/gossip_service.rs +++ b/core/src/gossip_service.rs @@ -3,9 +3,11 @@ use crate::bank_forks::BankForks; use crate::blocktree::Blocktree; use crate::cluster_info::ClusterInfo; +use crate::cluster_info::FULLNODE_PORT_RANGE; use crate::contact_info::ContactInfo; use crate::service::Service; use crate::streamer; +use solana_client::thin_client::{create_client, ThinClient}; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, KeypairUtil}; use std::net::SocketAddr; @@ -105,6 +107,14 @@ pub fn discover( )) } +pub fn get_clients(nodes: &[ContactInfo]) -> Vec { + nodes + .iter() + .filter_map(ContactInfo::valid_client_facing_addr) + .map(|addrs| create_client(addrs, FULLNODE_PORT_RANGE)) + .collect() +} + fn spy( spy_ref: Arc>, num_nodes: Option,