Skip to content

Commit

Permalink
Remove hash::new_with_thread_rng, use Hash::new_unique()
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Aug 17, 2023
1 parent a4b8dd2 commit 094bfec
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 56 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_with_thread_rng(),
solana_sdk::hash::Hash::new_unique(),
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_with_thread_rng)
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.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_with_thread_rng)
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1200)
.collect();
let mut fail = 0;
Expand Down
17 changes: 8 additions & 9 deletions bloom/src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ mod test {

#[test]
fn test_atomic_bloom() {
let hash_values: Vec<_> = std::iter::repeat_with(solana_sdk::hash::new_with_thread_rng)
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1200)
.collect();
let bloom: AtomicBloom<_> = Bloom::<Hash>::random(1287, 0.1, 7424).into();
Expand All @@ -327,7 +327,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_with_thread_rng)
let false_positive = std::iter::repeat_with(Hash::new_unique)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand All @@ -339,7 +339,7 @@ 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_with_thread_rng)
let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1000)
.collect();
for hash_value in &hash_values {
Expand Down Expand Up @@ -374,10 +374,9 @@ mod test {
assert!(bloom.contains(hash_value));
}
// Round trip, inserting new hash values.
let more_hash_values: Vec<_> =
std::iter::repeat_with(solana_sdk::hash::new_with_thread_rng)
.take(1000)
.collect();
let more_hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.take(1000)
.collect();
let bloom: AtomicBloom<_> = bloom.into();
assert_eq!(bloom.num_bits, 9731);
assert_eq!(bloom.bits.len(), (9731 + 63) / 64);
Expand All @@ -390,7 +389,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_with_thread_rng)
let false_positive = std::iter::repeat_with(Hash::new_unique)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand All @@ -409,7 +408,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_with_thread_rng)
let false_positive = std::iter::repeat_with(Hash::new_unique)
.take(10_000)
.filter(|hash_value| bloom.contains(hash_value))
.count();
Expand Down
4 changes: 2 additions & 2 deletions gossip/benches/crds_gossip_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use {
crds_gossip_pull::{CrdsFilter, CrdsGossipPull},
crds_value::CrdsValue,
},
solana_sdk::hash,
solana_sdk::hash::Hash,
std::sync::RwLock,
test::Bencher,
};

#[bench]
fn bench_hash_as_u64(bencher: &mut Bencher) {
let hashes: Vec<_> = std::iter::repeat_with(hash::new_with_thread_rng)
let hashes: Vec<_> = std::iter::repeat_with(Hash::new_unique)
.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 @@ -3848,7 +3848,7 @@ mod tests {
// add a vote
let vote = Vote::new(
vec![1, 3, 7], // slots
solana_sdk::hash::new_with_thread_rng(),
Hash::new_unique(),
);
let ix = vote_instruction::vote(
&Pubkey::new_unique(), // vote_pubkey
Expand Down Expand Up @@ -3878,7 +3878,7 @@ mod tests {
}

fn new_vote_transaction(slots: Vec<Slot>) -> Transaction {
let vote = Vote::new(slots, solana_sdk::hash::new_with_thread_rng());
let vote = Vote::new(slots, Hash::new_unique());
let ix = vote_instruction::vote(
&Pubkey::new_unique(), // vote_pubkey
&Pubkey::new_unique(), // authorized_voter_pubkey
Expand Down
8 changes: 3 additions & 5 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 {
out
}
for _ in 0..100 {
let hash = solana_sdk::hash::new_with_thread_rng();
let hash = Hash::new_unique();
assert_eq!(CrdsFilter::hash_as_u64(&hash), hash_as_u64_bitops(&hash));
}
}
Expand All @@ -666,7 +666,7 @@ pub(crate) mod tests {
let mask = CrdsFilter::compute_mask(0, filter.mask_bits);
assert_eq!(filter.mask, mask);
for _ in 0..10 {
let hash = solana_sdk::hash::new_with_thread_rng();
let hash = Hash::new_unique();
assert!(filter.test_mask(&hash));
}
}
Expand All @@ -675,9 +675,7 @@ pub(crate) mod tests {
fn test_crds_filter_set_add() {
let crds_filter_set =
CrdsFilterSet::new(/*num_items=*/ 9672788, /*max_bytes=*/ 8196);
let hash_values: Vec<_> = repeat_with(solana_sdk::hash::new_with_thread_rng)
.take(1024)
.collect();
let hash_values: Vec<_> = repeat_with(Hash::new_unique).take(1024).collect();
for hash_value in &hash_values {
crds_filter_set.add(*hash_value);
}
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_with_thread_rng();
let hash = Hash::new_unique();
(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_with_thread_rng(),
Hash::new_unique(),
);
let ix = vote_instruction::vote(
&Pubkey::new_unique(), // vote_pubkey
Expand Down
16 changes: 8 additions & 8 deletions gossip/src/duplicate_shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub(crate) mod tests {
solana_entry::entry::Entry,
solana_ledger::shred::{ProcessShredsStats, ReedSolomonCache, Shredder},
solana_sdk::{
hash,
hash::Hash,
signature::{Keypair, Signer},
system_transaction,
},
Expand Down 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_with_thread_rng(), // recent blockhash
&Keypair::new(), // from
&Pubkey::new_unique(), // to
rng.gen(), // lamports
Hash::new_unique(), // recent blockhash
);
Entry::new(
&hash::new_with_thread_rng(), // prev_hash
1, // num_hashes,
vec![tx], // transactions
&Hash::new_unique(), // prev_hash
1, // num_hashes,
vec![tx], // transactions
)
})
.take(5)
Expand Down
6 changes: 3 additions & 3 deletions ledger/src/shredder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ mod tests {
matches::assert_matches,
rand::{seq::SliceRandom, Rng},
solana_sdk::{
hash::{self, hash, Hash},
hash::{hash, Hash},
pubkey::Pubkey,
shred_version,
signature::{Signature, Signer},
Expand Down Expand Up @@ -994,8 +994,8 @@ mod tests {
.take(num_tx)
.collect();
let entry = Entry::new(
&hash::new_with_thread_rng(), // prev hash
rng.gen_range(1..64), // num hashes
&Hash::new_unique(), // prev hash
rng.gen_range(1..64), // num hashes
txs,
);
let keypair = Arc::new(Keypair::new());
Expand Down
14 changes: 6 additions & 8 deletions perf/src/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ 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_with_thread_rng);
let switch_proof_hash = rng.gen_bool(0.5).then(Hash::new_unique);
vote_transaction::new_vote_transaction(
slots,
solana_sdk::hash::new_with_thread_rng(), // bank_hash
solana_sdk::hash::new_with_thread_rng(), // blockhash
&Keypair::new(), // node_keypair
&Keypair::new(), // vote_keypair
&Keypair::new(), // authorized_voter_keypair
Hash::new_unique(), // bank_hash
Hash::new_unique(), // blockhash
&Keypair::new(), // node_keypair
&Keypair::new(), // vote_keypair
&Keypair::new(), // authorized_voter_keypair
switch_proof_hash,
)
}
6 changes: 3 additions & 3 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use {
fee::FeeStructure,
fee_calculator::FeeRateGovernor,
genesis_config::{create_genesis_config, ClusterType, GenesisConfig},
hash::{self, hash, Hash},
hash::{hash, Hash},
incinerator,
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
loader_upgradeable_instruction::UpgradeableLoaderInstruction,
Expand Down Expand Up @@ -9912,7 +9912,7 @@ fn test_verify_and_hash_transaction_sig_len() {
.remove(&feature_set::verify_tx_signatures_len::id());
let bank = Bank::new_for_tests(&genesis_config);

let recent_blockhash = hash::new_with_thread_rng();
let recent_blockhash = Hash::new_unique();
let from_keypair = Keypair::new();
let to_keypair = Keypair::new();
let from_pubkey = from_keypair.pubkey();
Expand Down Expand Up @@ -9968,7 +9968,7 @@ fn test_verify_transactions_packet_data_size() {
create_genesis_config_with_leader(42, &solana_sdk::pubkey::new_rand(), 42);
let bank = Bank::new_for_tests(&genesis_config);

let recent_blockhash = hash::new_with_thread_rng();
let recent_blockhash = Hash::new_unique();
let keypair = Keypair::new();
let pubkey = keypair.pubkey();
let make_transaction = |size| {
Expand Down
15 changes: 4 additions & 11 deletions sdk/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//! [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
//! [`Hash`]: struct@Hash
use rand::Rng;
pub use solana_program::hash::*;

/// Random hash value for tests and benchmarks.
#[deprecated(since = "1.17.0", note = "Please use `new_with_thread_rng`")]
#[deprecated(
since = "1.17.0",
note = "Please use `Hash::new_unique()` for testing, or fill 32 bytes with any source of randomness"
)]
#[cfg(feature = "full")]
pub fn new_rand<R: ?Sized>(rng: &mut R) -> Hash
where
Expand All @@ -17,12 +19,3 @@ where
rng.fill(&mut buf);
Hash::new(&buf)
}

/// Random hash value for tests and benchmarks.
#[cfg(feature = "full")]
pub fn new_with_thread_rng() -> Hash {
let mut rng = rand::thread_rng();
let mut buf = [0u8; HASH_BYTES];
rng.fill(&mut buf);
Hash::new(&buf)
}

0 comments on commit 094bfec

Please sign in to comment.