diff --git a/accounts-db/src/cache_hash_data.rs b/accounts-db/src/cache_hash_data.rs index e21f699749f317..d3aad6f88a064e 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_with_thread_rng(), + solana_sdk::hash::Hash::new_unique(), ct as u64, pk, ) diff --git a/bloom/benches/bloom.rs b/bloom/benches/bloom.rs index 88e5951ccbea73..522b45ee6b8963 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_with_thread_rng) + let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique) .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_with_thread_rng) + let hash_values: Vec<_> = std::iter::repeat_with(Hash::new_unique) .take(1200) .collect(); let mut fail = 0; diff --git a/bloom/src/bloom.rs b/bloom/src/bloom.rs index c198f385d88078..b76b6f43b01b16 100644 --- a/bloom/src/bloom.rs +++ b/bloom/src/bloom.rs @@ -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::::random(1287, 0.1, 7424).into(); @@ -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(); @@ -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::::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 { @@ -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); @@ -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(); @@ -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(); diff --git a/gossip/benches/crds_gossip_pull.rs b/gossip/benches/crds_gossip_pull.rs index 9c831a50ad752e..eaed9b671166ef 100644 --- a/gossip/benches/crds_gossip_pull.rs +++ b/gossip/benches/crds_gossip_pull.rs @@ -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(|| { diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index eab95a4acac02b..16da13238ac18a 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -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 @@ -3878,7 +3878,7 @@ mod tests { } fn new_vote_transaction(slots: Vec) -> 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 diff --git a/gossip/src/crds_gossip_pull.rs b/gossip/src/crds_gossip_pull.rs index bfd3bd6e75d1b6..3e69192f2ac7e9 100644 --- a/gossip/src/crds_gossip_pull.rs +++ b/gossip/src/crds_gossip_pull.rs @@ -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)); } } @@ -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)); } } @@ -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); } diff --git a/gossip/src/crds_value.rs b/gossip/src/crds_value.rs index 7e59347d7480ca..24d66e3c520b71 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_with_thread_rng(); + let hash = Hash::new_unique(); (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_with_thread_rng(), + Hash::new_unique(), ); 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 52f6e9bfa518a2..0d7d35d26afa5c 100644 --- a/gossip/src/duplicate_shred.rs +++ b/gossip/src/duplicate_shred.rs @@ -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, }, @@ -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) diff --git a/ledger/src/shredder.rs b/ledger/src/shredder.rs index 5488536bec8511..3a4b17515ced13 100644 --- a/ledger/src/shredder.rs +++ b/ledger/src/shredder.rs @@ -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}, @@ -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()); diff --git a/perf/src/test_tx.rs b/perf/src/test_tx.rs index 567a3aa7a20e0c..befbc83206b281 100644 --- a/perf/src/test_tx.rs +++ b/perf/src/test_tx.rs @@ -59,16 +59,14 @@ 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_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, ) } diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 5534f7769a8172..8e3da21e1c46dd 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -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, @@ -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(); @@ -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| { diff --git a/sdk/src/hash.rs b/sdk/src/hash.rs index 5e5d6d95b85c17..34aafbe1be7928 100644 --- a/sdk/src/hash.rs +++ b/sdk/src/hash.rs @@ -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(rng: &mut R) -> Hash where @@ -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) -}