From 5f9675a5957c12cb506dc6e499d032e63bcb19b8 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Thu, 17 Aug 2023 13:54:41 +0200 Subject: [PATCH] Replace `hash::new_rand` with `hash::new_with_thread_rng` Run: ``` git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/' ``` --- accounts-db/src/cache_hash_data.rs | 2 +- bloom/benches/bloom.rs | 4 ++-- bloom/src/bloom.rs | 22 ++++++++++++---------- gossip/benches/crds_gossip_pull.rs | 2 +- gossip/src/cluster_info.rs | 4 ++-- gossip/src/crds_gossip_pull.rs | 6 +++--- gossip/src/crds_value.rs | 4 ++-- gossip/src/duplicate_shred.rs | 14 +++++++------- ledger/src/shredder.rs | 4 ++-- perf/src/test_tx.rs | 14 ++++++++------ runtime/src/bank/tests.rs | 4 ++-- 11 files changed, 42 insertions(+), 38 deletions(-) diff --git a/accounts-db/src/cache_hash_data.rs b/accounts-db/src/cache_hash_data.rs index b0e869f7604bd1..e21f699749f317 100644 --- a/accounts-db/src/cache_hash_data.rs +++ b/accounts-db/src/cache_hash_data.rs @@ -462,7 +462,7 @@ pub mod tests { } CalculateHashIntermediate::new( - solana_sdk::hash::new_rand(&mut rng), + solana_sdk::hash::new_with_thread_rng(), ct as u64, pk, ) diff --git a/bloom/benches/bloom.rs b/bloom/benches/bloom.rs index 242b5d60b0f42f..ba57a62cedaac6 100644 --- a/bloom/benches/bloom.rs +++ b/bloom/benches/bloom.rs @@ -103,7 +103,7 @@ fn bench_sigs_hashmap(bencher: &mut Bencher) { #[bench] fn bench_add_hash(bencher: &mut Bencher) { let mut rng = rand::thread_rng(); - let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(1200) .collect(); let mut fail = 0; @@ -123,7 +123,7 @@ fn bench_add_hash(bencher: &mut Bencher) { #[bench] fn bench_add_hash_atomic(bencher: &mut Bencher) { let mut rng = rand::thread_rng(); - let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(1200) .collect(); let mut fail = 0; diff --git a/bloom/src/bloom.rs b/bloom/src/bloom.rs index c622c8a8604b03..323a6494050b0e 100644 --- a/bloom/src/bloom.rs +++ b/bloom/src/bloom.rs @@ -311,9 +311,10 @@ mod test { #[test] fn test_atomic_bloom() { let mut rng = rand::thread_rng(); - let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) - .take(1200) - .collect(); + let hash_values: Vec<_> = + std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) + .take(1200) + .collect(); let bloom: AtomicBloom<_> = Bloom::::random(1287, 0.1, 7424).into(); assert_eq!(bloom.keys.len(), 3); assert_eq!(bloom.num_bits, 6168); @@ -328,7 +329,7 @@ mod test { for hash_value in hash_values { assert!(bloom.contains(&hash_value)); } - let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(10_000) .filter(|hash_value| bloom.contains(hash_value)) .count(); @@ -340,9 +341,10 @@ mod test { let mut rng = rand::thread_rng(); let keys: Vec<_> = std::iter::repeat_with(|| rng.gen()).take(5).collect(); let mut bloom = Bloom::::new(9731, keys.clone()); - let hash_values: Vec<_> = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) - .take(1000) - .collect(); + let hash_values: Vec<_> = + std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) + .take(1000) + .collect(); for hash_value in &hash_values { bloom.add(hash_value); } @@ -376,7 +378,7 @@ mod test { } // Round trip, inserting new hash values. let more_hash_values: Vec<_> = - std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(1000) .collect(); let bloom: AtomicBloom<_> = bloom.into(); @@ -391,7 +393,7 @@ mod test { for hash_value in &more_hash_values { assert!(bloom.contains(hash_value)); } - let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(10_000) .filter(|hash_value| bloom.contains(hash_value)) .count(); @@ -410,7 +412,7 @@ mod test { for hash_value in &more_hash_values { assert!(bloom.contains(hash_value)); } - let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let false_positive = std::iter::repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(10_000) .filter(|hash_value| bloom.contains(hash_value)) .count(); diff --git a/gossip/benches/crds_gossip_pull.rs b/gossip/benches/crds_gossip_pull.rs index 2f954695725fd6..1cd3a67233b515 100644 --- a/gossip/benches/crds_gossip_pull.rs +++ b/gossip/benches/crds_gossip_pull.rs @@ -19,7 +19,7 @@ use { #[bench] fn bench_hash_as_u64(bencher: &mut Bencher) { let mut rng = thread_rng(); - let hashes: Vec<_> = std::iter::repeat_with(|| hash::new_rand(&mut rng)) + let hashes: Vec<_> = std::iter::repeat_with(|| hash::new_with_thread_rng()) .take(1000) .collect(); bencher.iter(|| { diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index ce05704cdec8bb..30bb61f9f7107a 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -3849,7 +3849,7 @@ mod tests { // add a vote let vote = Vote::new( vec![1, 3, 7], // slots - solana_sdk::hash::new_rand(&mut rng), + solana_sdk::hash::new_with_thread_rng(), ); let ix = vote_instruction::vote( &Pubkey::new_unique(), // vote_pubkey @@ -3879,7 +3879,7 @@ mod tests { } fn new_vote_transaction(rng: &mut R, slots: Vec) -> Transaction { - let vote = Vote::new(slots, solana_sdk::hash::new_rand(rng)); + let vote = Vote::new(slots, solana_sdk::hash::new_with_thread_rng()); let ix = vote_instruction::vote( &Pubkey::new_unique(), // vote_pubkey &Pubkey::new_unique(), // authorized_voter_pubkey diff --git a/gossip/src/crds_gossip_pull.rs b/gossip/src/crds_gossip_pull.rs index acb39fa5bd83dd..4f609d1218ec7b 100644 --- a/gossip/src/crds_gossip_pull.rs +++ b/gossip/src/crds_gossip_pull.rs @@ -655,7 +655,7 @@ pub(crate) mod tests { } let mut rng = thread_rng(); for _ in 0..100 { - let hash = solana_sdk::hash::new_rand(&mut rng); + let hash = solana_sdk::hash::new_with_thread_rng(); assert_eq!(CrdsFilter::hash_as_u64(&hash), hash_as_u64_bitops(&hash)); } } @@ -667,7 +667,7 @@ pub(crate) mod tests { assert_eq!(filter.mask, mask); let mut rng = thread_rng(); for _ in 0..10 { - let hash = solana_sdk::hash::new_rand(&mut rng); + let hash = solana_sdk::hash::new_with_thread_rng(); assert!(filter.test_mask(&hash)); } } @@ -677,7 +677,7 @@ pub(crate) mod tests { let mut rng = thread_rng(); let crds_filter_set = CrdsFilterSet::new(/*num_items=*/ 9672788, /*max_bytes=*/ 8196); - let hash_values: Vec<_> = repeat_with(|| solana_sdk::hash::new_rand(&mut rng)) + let hash_values: Vec<_> = repeat_with(|| solana_sdk::hash::new_with_thread_rng()) .take(1024) .collect(); for hash_value in &hash_values { diff --git a/gossip/src/crds_value.rs b/gossip/src/crds_value.rs index 167c568a6eb2e2..7e59347d7480ca 100644 --- a/gossip/src/crds_value.rs +++ b/gossip/src/crds_value.rs @@ -198,7 +198,7 @@ impl AccountsHashes { let num_hashes = rng.gen_range(0..MAX_LEGACY_SNAPSHOT_HASHES) + 1; let hashes = std::iter::repeat_with(|| { let slot = 47825632 + rng.gen_range(0..512); - let hash = solana_sdk::hash::new_rand(rng); + let hash = solana_sdk::hash::new_with_thread_rng(); (slot, hash) }) .take(num_hashes) @@ -801,7 +801,7 @@ mod test { let mut rng = rand::thread_rng(); let vote = vote_state::Vote::new( vec![1, 3, 7], // slots - solana_sdk::hash::new_rand(&mut rng), + solana_sdk::hash::new_with_thread_rng(), ); let ix = vote_instruction::vote( &Pubkey::new_unique(), // vote_pubkey diff --git a/gossip/src/duplicate_shred.rs b/gossip/src/duplicate_shred.rs index 26d39634b760a3..52f6e9bfa518a2 100644 --- a/gossip/src/duplicate_shred.rs +++ b/gossip/src/duplicate_shred.rs @@ -299,15 +299,15 @@ pub(crate) mod tests { ) -> Shred { let entries: Vec<_> = std::iter::repeat_with(|| { let tx = system_transaction::transfer( - &Keypair::new(), // from - &Pubkey::new_unique(), // to - rng.gen(), // lamports - hash::new_rand(rng), // recent blockhash + &Keypair::new(), // from + &Pubkey::new_unique(), // to + rng.gen(), // lamports + hash::new_with_thread_rng(), // recent blockhash ); Entry::new( - &hash::new_rand(rng), // prev_hash - 1, // num_hashes, - vec![tx], // transactions + &hash::new_with_thread_rng(), // prev_hash + 1, // num_hashes, + vec![tx], // transactions ) }) .take(5) diff --git a/ledger/src/shredder.rs b/ledger/src/shredder.rs index 97a29b79bd2ac6..5488536bec8511 100644 --- a/ledger/src/shredder.rs +++ b/ledger/src/shredder.rs @@ -994,8 +994,8 @@ mod tests { .take(num_tx) .collect(); let entry = Entry::new( - &hash::new_rand(&mut rng), // prev hash - rng.gen_range(1..64), // num hashes + &hash::new_with_thread_rng(), // prev hash + rng.gen_range(1..64), // num hashes txs, ); let keypair = Arc::new(Keypair::new()); diff --git a/perf/src/test_tx.rs b/perf/src/test_tx.rs index 4e896b547b96af..53415c838b295c 100644 --- a/perf/src/test_tx.rs +++ b/perf/src/test_tx.rs @@ -59,14 +59,16 @@ where let mut slots: Vec = std::iter::repeat_with(|| rng.gen()).take(5).collect(); slots.sort_unstable(); slots.dedup(); - let switch_proof_hash = rng.gen_bool(0.5).then(|| solana_sdk::hash::new_rand(rng)); + let switch_proof_hash = rng + .gen_bool(0.5) + .then(|| solana_sdk::hash::new_with_thread_rng()); vote_transaction::new_vote_transaction( slots, - solana_sdk::hash::new_rand(rng), // bank_hash - solana_sdk::hash::new_rand(rng), // blockhash - &Keypair::generate(rng), // node_keypair - &Keypair::generate(rng), // vote_keypair - &Keypair::generate(rng), // authorized_voter_keypair + solana_sdk::hash::new_with_thread_rng(), // bank_hash + solana_sdk::hash::new_with_thread_rng(), // blockhash + &Keypair::generate(rng), // node_keypair + &Keypair::generate(rng), // vote_keypair + &Keypair::generate(rng), // authorized_voter_keypair switch_proof_hash, ) } diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index f186eb045a705e..8e5f0dbedacb2b 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -9913,7 +9913,7 @@ fn test_verify_and_hash_transaction_sig_len() { let bank = Bank::new_for_tests(&genesis_config); let mut rng = rand::thread_rng(); - let recent_blockhash = hash::new_rand(&mut rng); + let recent_blockhash = hash::new_with_thread_rng(); let from_keypair = Keypair::new(); let to_keypair = Keypair::new(); let from_pubkey = from_keypair.pubkey(); @@ -9970,7 +9970,7 @@ fn test_verify_transactions_packet_data_size() { let bank = Bank::new_for_tests(&genesis_config); let mut rng = rand::thread_rng(); - let recent_blockhash = hash::new_rand(&mut rng); + let recent_blockhash = hash::new_with_thread_rng(); let keypair = Keypair::new(); let pubkey = keypair.pubkey(); let make_transaction = |size| {