From 93e83ee6cc9503b0692a63626ef2ba4e05f5050a Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 10 Jan 2020 15:54:07 -0700 Subject: [PATCH] Pick an RPC node at random to avoid getting stuck on a bad RPC node (cherry picked from commit ad4d41e6024a34801ff651f25dd4a2de6230993c) --- Cargo.lock | 1 + validator/Cargo.toml | 1 + validator/src/main.rs | 8 +++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f545fa0e7b0eee..048da1c16d09c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4031,6 +4031,7 @@ dependencies = [ "gag 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "indicatif 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", "solana-clap-utils 0.21.8", diff --git a/validator/Cargo.toml b/validator/Cargo.toml index c65497ef614014..29dd1c276d9fd0 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -15,6 +15,7 @@ chrono = { version = "0.4.10", features = ["serde"] } console = "0.9.1" log = "0.4.8" indicatif = "0.13.0" +rand = "0.6.5" reqwest = { version = "0.9.22", default-features = false } serde_json = "1.0.41" solana-clap-utils = { path = "../clap-utils", version = "0.21.8" } diff --git a/validator/src/main.rs b/validator/src/main.rs index 86fc793d0e026e..9598080213b81c 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -3,6 +3,7 @@ use clap::{crate_description, crate_name, value_t, value_t_or_exit, App, Arg}; use console::{style, Emoji}; use indicatif::{ProgressBar, ProgressStyle}; use log::*; +use rand::{thread_rng, Rng}; use solana_clap_utils::{ input_parsers::pubkey_of, input_validators::{is_keypair, is_pubkey_or_keypair}, @@ -217,15 +218,16 @@ fn get_rpc_addr( .any(|contact_info| contact_info.gossip == *entrypoint_gossip); if found_entrypoint & !rpc_peers.is_empty() { - // Prefer the entrypoint's RPC service it it has one, otherwise pick the first RPC - // service found + // Prefer the entrypoint's RPC service if present, otherwise pick a node at random if let Some(contact_info) = rpc_peers .iter() .find(|contact_info| contact_info.gossip == *entrypoint_gossip) { break (contact_info.id, contact_info.rpc); } - break (rpc_peers[0].id, rpc_peers[0].rpc); + + let i = thread_rng().gen_range(0, rpc_peers.len()); + break (rpc_peers[i].id, rpc_peers[i].rpc); } sleep(Duration::from_secs(1));