Skip to content

Commit

Permalink
Replace hash::new_rand with hash::new_with_thread_rng
Browse files Browse the repository at this point in the history
Run:
```
git grep -l hash::new_rand | xargs sed -i'' -e 's/hash::new_rand([^)]*/hash::new_with_thread_rng(/'
```
  • Loading branch information
joncinque committed Aug 17, 2023
1 parent f78cef6 commit 5f9675a
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion accounts-db/src/cache_hash_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions bloom/benches/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions bloom/src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Hash>::random(1287, 0.1, 7424).into();
assert_eq!(bloom.keys.len(), 3);
assert_eq!(bloom.num_bits, 6168);
Expand All @@ -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();
Expand All @@ -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::<Hash>::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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion gossip/benches/crds_gossip_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down
4 changes: 2 additions & 2 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3879,7 +3879,7 @@ mod tests {
}

fn new_vote_transaction<R: Rng>(rng: &mut R, slots: Vec<Slot>) -> 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
Expand Down
6 changes: 3 additions & 3 deletions gossip/src/crds_gossip_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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));
}
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions gossip/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions gossip/src/duplicate_shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions ledger/src/shredder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 8 additions & 6 deletions perf/src/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ where
let mut slots: Vec<Slot> = 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,
)
}
4 changes: 2 additions & 2 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit 5f9675a

Please sign in to comment.