diff --git a/Cargo.lock b/Cargo.lock index 7b10822718..cf63fcb4e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1750,7 +1750,8 @@ dependencies = [ "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "pnet_datalink 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", "ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1944,7 +1945,7 @@ dependencies = [ "influx_db_client 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", "solana-sdk 0.11.0", ] diff --git a/Cargo.toml b/Cargo.toml index fbdd148846..b801450ecf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,7 +86,8 @@ log = "0.4.2" matches = "0.1.6" nix = "0.12.0" pnet_datalink = "0.21.0" -rand = "0.5.1" +rand = "0.6.1" +rand_chacha = "0.1.0" rayon = "1.0.0" reqwest = "0.9.0" ring = "0.13.2" diff --git a/benches/db_ledger.rs b/benches/db_ledger.rs index c8ac35e4bf..1782100fb4 100644 --- a/benches/db_ledger.rs +++ b/benches/db_ledger.rs @@ -4,7 +4,7 @@ extern crate rocksdb; extern crate solana; extern crate test; -use rand::distributions::{Distribution, Range}; +use rand::seq::SliceRandom; use rand::{thread_rng, Rng}; use rocksdb::{Options, DB}; use solana::db_ledger::{DataCf, DbLedger, LedgerColumnFamilyRaw}; @@ -94,13 +94,10 @@ fn bench_read_sequential(bench: &mut Bencher) { setup_read_bench(&mut db_ledger, num_small_blobs, num_large_blobs, slot); let num_reads = total_blobs / 15; - // Make range [0, total_blobs - 1] - let range = Range::new(0, num_small_blobs + num_large_blobs); - let mut rng = rand::thread_rng(); bench.iter(move || { - // Generate random starting point in that range, read num_reads blobs sequentially - let start_index = range.sample(&mut rng); + // Generate random starting point in the range [0, total_blobs - 1], read num_reads blobs sequentially + let start_index = rng.gen_range(0, num_small_blobs + num_large_blobs); for i in start_index..start_index + num_reads { let _ = db_ledger @@ -129,14 +126,11 @@ fn bench_read_random(bench: &mut Bencher) { let num_reads = total_blobs / 15; - // Make range [0, total_blobs - 1] - let range = Range::new(0, total_blobs); - // Generate a num_reads sized random sample of indexes in range [0, total_blobs - 1], // simulating random reads let mut rng = rand::thread_rng(); let indexes: Vec = (0..num_reads) - .map(|_| range.sample(&mut rng) as usize) + .map(|_| rng.gen_range(0, total_blobs) as usize) .collect(); bench.iter(move || { for i in indexes.iter() { @@ -161,7 +155,7 @@ fn bench_insert_data_blob_small(bench: &mut Bencher) { let shared_blobs = entries.to_blobs(); let mut blob_locks: Vec<_> = shared_blobs.iter().map(|b| b.write().unwrap()).collect(); let mut blobs: Vec<&mut Blob> = blob_locks.iter_mut().map(|b| &mut **b).collect(); - thread_rng().shuffle(&mut blobs); + blobs.shuffle(&mut thread_rng()); let slot = 0; bench.iter(move || { @@ -188,7 +182,7 @@ fn bench_insert_data_blob_big(bench: &mut Bencher) { let shared_blobs = entries.to_blobs(); let mut blob_locks: Vec<_> = shared_blobs.iter().map(|b| b.write().unwrap()).collect(); let mut blobs: Vec<&mut Blob> = blob_locks.iter_mut().map(|b| &mut **b).collect(); - thread_rng().shuffle(&mut blobs); + blobs.shuffle(&mut thread_rng()); let slot = 0; bench.iter(move || { diff --git a/metrics/Cargo.toml b/metrics/Cargo.toml index 75fb034ee8..ac33c106fb 100644 --- a/metrics/Cargo.toml +++ b/metrics/Cargo.toml @@ -9,7 +9,7 @@ license = "Apache-2.0" [dependencies] influx_db_client = "0.3.6" log = "0.4.2" -rand = "0.5.1" +rand = "0.6.1" reqwest = "0.9.0" lazy_static = "1.2.0" solana-sdk = { path = "../sdk", version = "0.11.0" } diff --git a/src/crds_gossip_pull.rs b/src/crds_gossip_pull.rs index c291157280..a022691740 100644 --- a/src/crds_gossip_pull.rs +++ b/src/crds_gossip_pull.rs @@ -17,7 +17,7 @@ use crds_gossip_error::CrdsGossipError; use crds_value::{CrdsValue, CrdsValueLabel}; use packet::BLOB_DATA_SIZE; use rand; -use rand::distributions::{Distribution, Weighted, WeightedChoice}; +use rand::distributions::{Distribution, WeightedIndex}; use solana_sdk::hash::Hash; use solana_sdk::pubkey::Pubkey; use std::cmp; @@ -54,7 +54,7 @@ impl CrdsGossipPull { self_id: Pubkey, now: u64, ) -> Result<(Pubkey, Bloom, CrdsValue), CrdsGossipError> { - let mut options: Vec<_> = crds + let options: Vec<_> = crds .table .values() .filter_map(|v| v.value.contact_info()) @@ -66,17 +66,18 @@ impl CrdsGossipPull { 1, cmp::min(u64::from(u16::max_value()) - 1, (now - req_time) / 1024) as u32, ); - Weighted { weight, item } + (weight, item) }).collect(); if options.is_empty() { return Err(CrdsGossipError::NoPeers); } let filter = self.build_crds_filter(crds); - let random = WeightedChoice::new(&mut options).sample(&mut rand::thread_rng()); + let index = WeightedIndex::new(options.iter().map(|weighted| weighted.0)).unwrap(); + let random = index.sample(&mut rand::thread_rng()); let self_info = crds .lookup(&CrdsValueLabel::ContactInfo(self_id)) .unwrap_or_else(|| panic!("self_id invalid {}", self_id)); - Ok((random.id, filter, self_info.clone())) + Ok((options[random].1.id, filter, self_info.clone())) } /// time when a request to `from` was initiated diff --git a/src/crds_gossip_push.rs b/src/crds_gossip_push.rs index 3d793c69f1..94b8409603 100644 --- a/src/crds_gossip_push.rs +++ b/src/crds_gossip_push.rs @@ -17,7 +17,8 @@ use crds_gossip_error::CrdsGossipError; use crds_value::{CrdsValue, CrdsValueLabel}; use indexmap::map::IndexMap; use packet::BLOB_DATA_SIZE; -use rand::{self, Rng}; +use rand; +use rand::seq::SliceRandom; use solana_sdk::hash::Hash; use solana_sdk::pubkey::Pubkey; use std::cmp; @@ -97,7 +98,7 @@ impl CrdsGossipPush { pub fn new_push_messages(&mut self, crds: &Crds, now: u64) -> (Vec, Vec) { let max = self.active_set.len(); let mut nodes: Vec<_> = (0..max).collect(); - rand::thread_rng().shuffle(&mut nodes); + nodes.shuffle(&mut rand::thread_rng()); let peers: Vec = nodes .into_iter() .filter_map(|n| self.active_set.get_index(n)) @@ -165,7 +166,7 @@ impl CrdsGossipPush { let need = Self::compute_need(self.num_active, self.active_set.len(), ratio); let mut new_items = HashMap::new(); let mut ixs: Vec<_> = (0..crds.table.len()).collect(); - rand::thread_rng().shuffle(&mut ixs); + ixs.shuffle(&mut rand::thread_rng()); for ix in ixs { let item = crds.table.get_index(ix); @@ -195,7 +196,7 @@ impl CrdsGossipPush { } } let mut keys: Vec = self.active_set.keys().cloned().collect(); - rand::thread_rng().shuffle(&mut keys); + keys.shuffle(&mut rand::thread_rng()); let num = keys.len() / ratio; for k in &keys[..num] { self.active_set.remove(k); diff --git a/src/lib.rs b/src/lib.rs index 49516bffda..5ebecbddbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,3 +130,4 @@ extern crate untrusted; extern crate matches; extern crate rand; +extern crate rand_chacha; diff --git a/src/signature.rs b/src/signature.rs index 8cb5d3ca9b..5782ed9898 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,6 +1,7 @@ //! The `signature` module provides functionality for public, and private keys. -use rand::{ChaChaRng, Rng, SeedableRng}; +use rand::{Rng, SeedableRng}; +use rand_chacha::ChaChaRng; use rayon::prelude::*; use solana_sdk::signature::Keypair; use untrusted::Input; diff --git a/src/storage_stage.rs b/src/storage_stage.rs index 030682a210..57ee1c1693 100644 --- a/src/storage_stage.rs +++ b/src/storage_stage.rs @@ -5,7 +5,8 @@ #[cfg(all(feature = "chacha", feature = "cuda"))] use chacha_cuda::chacha_cbc_encrypt_file_many_keys; use entry::EntryReceiver; -use rand::{ChaChaRng, Rng, SeedableRng}; +use rand::{Rng, SeedableRng}; +use rand_chacha::ChaChaRng; use result::{Error, Result}; use service::Service; use solana_sdk::hash::Hash;