Pick an RPC node at random to avoid getting stuck on a bad RPC node

This commit is contained in:
Michael Vines 2020-01-10 15:54:07 -07:00
parent 9754fc789e
commit ad4d41e602
3 changed files with 7 additions and 3 deletions

1
Cargo.lock generated
View File

@ -4347,6 +4347,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.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-clap-utils 0.23.0",

View File

@ -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.10.1", default-features = false, features = ["blocking"] }
serde_json = "1.0.44"
solana-clap-utils = { path = "../clap-utils", version = "0.23.0" }

View File

@ -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));