Skip to content

Commit d2299be

Browse files
authored
deprecates Hash::new in favor of Hash::new_from_array (anza-xyz#3617)
Hash::new lacks type-safety and may panic.
1 parent 8b720f7 commit d2299be

File tree

29 files changed

+103
-71
lines changed

29 files changed

+103
-71
lines changed

account-decoder/src/parse_sysvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ mod test {
276276

277277
#[test]
278278
fn test_parse_sysvars() {
279-
let hash = Hash::new(&[1; 32]);
279+
let hash = Hash::new_from_array([1; 32]);
280280

281281
let clock_sysvar = create_account_for_test(&Clock::default());
282282
assert_eq!(

accounts-db/src/accounts_db/tests.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ define_accounts_db_test!(test_maybe_unref_accounts_already_in_ancient, |db| {
380380
rent_epoch: 0,
381381
};
382382
let offset = 3 * std::mem::size_of::<u64>();
383-
let hash = AccountHash(Hash::new(&[2; 32]));
383+
let hash = AccountHash(Hash::new_from_array([2; 32]));
384384
let stored_meta = StoredMeta {
385385
// global write version
386386
write_version_obsolete: 0,
@@ -2465,7 +2465,10 @@ fn test_verify_accounts_hash() {
24652465

24662466
db.set_accounts_hash(
24672467
some_slot,
2468-
(AccountsHash(Hash::new(&[0xca; HASH_BYTES])), capitalization),
2468+
(
2469+
AccountsHash(Hash::new_from_array([0xca; HASH_BYTES])),
2470+
capitalization,
2471+
),
24692472
);
24702473

24712474
assert_matches!(

accounts-db/src/accounts_hash.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,9 @@ mod tests {
14721472
// 0 hashes
14731473
let mut file = AccountHashesFile::new(0, dir_for_temp_cache_files.path());
14741474
assert!(file.get_reader().is_none());
1475-
let hashes = (0..2).map(|i| Hash::new(&[i; 32])).collect::<Vec<_>>();
1475+
let hashes = (0..2)
1476+
.map(|i| Hash::new_from_array([i; 32]))
1477+
.collect::<Vec<_>>();
14761478

14771479
// 1 hash
14781480
let mut file = AccountHashesFile::new(1, dir_for_temp_cache_files.path());
@@ -1493,7 +1495,9 @@ mod tests {
14931495
fn test_cumulative_hashes_from_files() {
14941496
let dir_for_temp_cache_files = tempdir().unwrap();
14951497
(0..4).for_each(|permutation| {
1496-
let hashes = (0..2).map(|i| Hash::new(&[i + 1; 32])).collect::<Vec<_>>();
1498+
let hashes = (0..2)
1499+
.map(|i| Hash::new_from_array([i + 1; 32]))
1500+
.collect::<Vec<_>>();
14971501

14981502
let mut combined = Vec::default();
14991503

@@ -1585,7 +1589,7 @@ mod tests {
15851589
let mut account_maps = Vec::new();
15861590

15871591
let pubkey = Pubkey::from([11u8; 32]);
1588-
let hash = AccountHash(Hash::new(&[1u8; 32]));
1592+
let hash = AccountHash(Hash::new_from_array([1u8; 32]));
15891593
let val = CalculateHashIntermediate {
15901594
hash,
15911595
lamports: 88,
@@ -1595,7 +1599,7 @@ mod tests {
15951599

15961600
// 2nd key - zero lamports, so will be removed
15971601
let pubkey = Pubkey::from([12u8; 32]);
1598-
let hash = AccountHash(Hash::new(&[2u8; 32]));
1602+
let hash = AccountHash(Hash::new_from_array([2u8; 32]));
15991603
let val = CalculateHashIntermediate {
16001604
hash,
16011605
lamports: 0,
@@ -1615,7 +1619,7 @@ mod tests {
16151619

16161620
// 3rd key - with pubkey value before 1st key so it will be sorted first
16171621
let pubkey = Pubkey::from([10u8; 32]);
1618-
let hash = AccountHash(Hash::new(&[2u8; 32]));
1622+
let hash = AccountHash(Hash::new_from_array([2u8; 32]));
16191623
let val = CalculateHashIntermediate {
16201624
hash,
16211625
lamports: 20,
@@ -1633,7 +1637,7 @@ mod tests {
16331637

16341638
// 3rd key - with later slot
16351639
let pubkey = Pubkey::from([10u8; 32]);
1636-
let hash = AccountHash(Hash::new(&[99u8; 32]));
1640+
let hash = AccountHash(Hash::new_from_array([99u8; 32]));
16371641
let val = CalculateHashIntermediate {
16381642
hash,
16391643
lamports: 30,
@@ -1729,7 +1733,7 @@ mod tests {
17291733
let key_b = Pubkey::from([2u8; 32]);
17301734
let key_c = Pubkey::from([3u8; 32]);
17311735
const COUNT: usize = 6;
1732-
let hashes = (0..COUNT).map(|i| AccountHash(Hash::new(&[i as u8; 32])));
1736+
let hashes = (0..COUNT).map(|i| AccountHash(Hash::new_from_array([i as u8; 32])));
17331737
// create this vector
17341738
// abbbcc
17351739
let keys = [key_a, key_b, key_b, key_b, key_c, key_c];
@@ -2392,7 +2396,8 @@ mod tests {
23922396
let mut input: Vec<_> = (0..count)
23932397
.map(|i| {
23942398
let key = Pubkey::from([(pass * iterations + count) as u8; 32]);
2395-
let hash = Hash::new(&[(pass * iterations + count + i + 1) as u8; 32]);
2399+
let hash =
2400+
Hash::new_from_array([(pass * iterations + count + i + 1) as u8; 32]);
23962401
(key, hash)
23972402
})
23982403
.collect();
@@ -2436,12 +2441,12 @@ mod tests {
24362441
let offset = 2;
24372442
let input = vec![
24382443
CalculateHashIntermediate {
2439-
hash: AccountHash(Hash::new(&[1u8; 32])),
2444+
hash: AccountHash(Hash::new_from_array([1u8; 32])),
24402445
lamports: u64::MAX - offset,
24412446
pubkey: Pubkey::new_unique(),
24422447
},
24432448
CalculateHashIntermediate {
2444-
hash: AccountHash(Hash::new(&[2u8; 32])),
2449+
hash: AccountHash(Hash::new_from_array([2u8; 32])),
24452450
lamports: offset + 1,
24462451
pubkey: Pubkey::new_unique(),
24472452
},
@@ -2470,12 +2475,12 @@ mod tests {
24702475
let offset = 2;
24712476
let input = vec![
24722477
vec![CalculateHashIntermediate {
2473-
hash: AccountHash(Hash::new(&[1u8; 32])),
2478+
hash: AccountHash(Hash::new_from_array([1u8; 32])),
24742479
lamports: u64::MAX - offset,
24752480
pubkey: Pubkey::new_unique(),
24762481
}],
24772482
vec![CalculateHashIntermediate {
2478-
hash: AccountHash(Hash::new(&[2u8; 32])),
2483+
hash: AccountHash(Hash::new_from_array([2u8; 32])),
24792484
lamports: offset + 1,
24802485
pubkey: Pubkey::new_unique(),
24812486
}],

accounts-db/src/ancient_append_vecs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@ pub mod tests {
24562456
rent_epoch: 0,
24572457
};
24582458
let offset = 3 * std::mem::size_of::<u64>();
2459-
let hash = AccountHash(Hash::new(&[2; 32]));
2459+
let hash = AccountHash(Hash::new_from_array([2; 32]));
24602460
let stored_meta = StoredMeta {
24612461
// global write version
24622462
write_version_obsolete: 0,

cli-output/src/cli_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3272,7 +3272,7 @@ mod tests {
32723272
));
32733273

32743274
let signers = vec![present.as_ref(), absent.as_ref(), bad.as_ref()];
3275-
let blockhash = Hash::new(&[7u8; 32]);
3275+
let blockhash = Hash::new_from_array([7u8; 32]);
32763276
tx.try_partial_sign(&signers, blockhash).unwrap();
32773277
let res = return_signers(&tx, &OutputFormat::JsonCompact).unwrap();
32783278
let sign_only = parse_sign_only_reply_string(&res);

cli/src/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ mod tests {
25632563
);
25642564

25652565
//Test Transfer Subcommand, offline sign
2566-
let blockhash = Hash::new(&[1u8; 32]);
2566+
let blockhash = Hash::new_from_array([1u8; 32]);
25672567
let blockhash_string = blockhash.to_string();
25682568
let test_transfer = test_commands.clone().get_matches_from(vec![
25692569
"test",

cli/src/nonce.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ mod tests {
11381138
let mut nonce_account = nonce_account::create_account(1).into_inner();
11391139
assert_eq!(state_from_account(&nonce_account), Ok(State::Uninitialized));
11401140

1141-
let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
1141+
let durable_nonce = DurableNonce::from_blockhash(&Hash::new_from_array([42u8; 32]));
11421142
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
11431143
nonce_account
11441144
.set_state(&Versions::new(State::Initialized(data.clone())))
@@ -1168,7 +1168,7 @@ mod tests {
11681168
Err(Error::InvalidStateForOperation)
11691169
);
11701170

1171-
let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
1171+
let durable_nonce = DurableNonce::from_blockhash(&Hash::new_from_array([42u8; 32]));
11721172
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
11731173
nonce_account
11741174
.set_state(&Versions::new(State::Initialized(data.clone())))

cli/src/stake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4127,7 +4127,7 @@ mod tests {
41274127
let offline_string = offline_pubkey.to_string();
41284128
let offline_sig = offline.sign_message(&[3u8]);
41294129
let offline_signer = format!("{offline_pubkey}={offline_sig}");
4130-
let nonce_hash = Hash::new(&[4u8; 32]);
4130+
let nonce_hash = Hash::new_from_array([4u8; 32]);
41314131
let nonce_hash_string = nonce_hash.to_string();
41324132
let test_create_stake_account2 = test_commands.clone().get_matches_from(vec![
41334133
"test",
@@ -4984,7 +4984,7 @@ mod tests {
49844984
let stake_auth_string = stake_auth_pubkey.to_string();
49854985
let stake_sig = stake_auth.sign_message(&[0u8]);
49864986
let stake_signer = format!("{stake_auth_pubkey}={stake_sig}");
4987-
let nonce_hash = Hash::new(&[4u8; 32]);
4987+
let nonce_hash = Hash::new_from_array([4u8; 32]);
49884988
let nonce_hash_string = nonce_hash.to_string();
49894989

49904990
let test_split_stake_account = test_commands.clone().get_matches_from(vec![

core/src/optimistic_confirmation_verifier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mod test {
186186
let snapshot_start_slot = 0;
187187
let mut optimistic_confirmation_verifier =
188188
OptimisticConfirmationVerifier::new(snapshot_start_slot);
189-
let bad_bank_hash = Hash::new(&[42u8; 32]);
189+
let bad_bank_hash = Hash::new_from_array([42u8; 32]);
190190
let blockstore_path = get_tmp_ledger_path_auto_delete!();
191191
let blockstore = Blockstore::open(blockstore_path.path()).unwrap();
192192
let optimistic_slots = vec![(1, bad_bank_hash), (3, Hash::default())];

entry/src/entry.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ impl EntrySlice for [Entry] {
747747
.all(|(j, ref_entry)| {
748748
let start = j * HASH_BYTES;
749749
let end = start + HASH_BYTES;
750-
let hash = Hash::new(&chunk[start..end]);
750+
let hash = <[u8; HASH_BYTES]>::try_from(&chunk[start..end])
751+
.map(Hash::new_from_array)
752+
.unwrap();
751753
compare_hashes(hash, ref_entry)
752754
})
753755
})

faucet/src/faucet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ mod tests {
650650
#[test]
651651
fn test_process_faucet_request() {
652652
let to = solana_sdk::pubkey::new_rand();
653-
let blockhash = Hash::new(to.as_ref());
653+
let blockhash = Hash::new_from_array(to.to_bytes());
654654
let lamports = 50;
655655
let req = FaucetRequest::GetAirdrop {
656656
lamports,

faucet/tests/local-faucet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn test_local_faucet() {
1414
let keypair = Keypair::new();
1515
let to = solana_sdk::pubkey::new_rand();
1616
let lamports = 50;
17-
let blockhash = Hash::new(to.as_ref());
17+
let blockhash = Hash::new_from_array(to.to_bytes());
1818
let create_instruction = system_instruction::transfer(&keypair.pubkey(), &to, lamports);
1919
let message = Message::new(&[create_instruction], Some(&keypair.pubkey()));
2020
let expected_tx = Transaction::new(&[&keypair], message, blockhash);

gossip/src/crds_gossip_pull.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ pub(crate) mod tests {
684684

685685
#[test]
686686
fn test_hash_as_u64() {
687-
let arr: Vec<u8> = (0..HASH_BYTES).map(|i| i as u8 + 1).collect();
688-
let hash = Hash::new(&arr);
687+
let arr: [u8; HASH_BYTES] = std::array::from_fn(|i| i as u8 + 1);
688+
let hash = Hash::new_from_array(arr);
689689
assert_eq!(CrdsFilter::hash_as_u64(&hash), 0x807060504030201);
690690
}
691691

ledger/src/shred.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,11 @@ pub mod layout {
785785
.ok()?;
786786
shred
787787
.get(offset..offset + SIZE_OF_MERKLE_ROOT)
788-
.map(Hash::new)
788+
.map(|merkle_root| {
789+
<[u8; SIZE_OF_MERKLE_ROOT]>::try_from(merkle_root)
790+
.map(Hash::new_from_array)
791+
.unwrap()
792+
})
789793
}
790794

791795
fn get_retransmitter_signature_offset(shred: &[u8]) -> Result<usize, Error> {
@@ -1404,19 +1408,19 @@ mod tests {
14041408
0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0x5a, 0x5a,
14051409
0xa5, 0xa5, 0x5a, 0x5a,
14061410
];
1407-
let version = shred_version::version_from_hash(&Hash::new(&hash));
1411+
let version = shred_version::version_from_hash(&Hash::new_from_array(hash));
14081412
assert_eq!(version, 1);
14091413
let hash = [
14101414
0xa5u8, 0xa5, 0x5a, 0x5a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14111415
0, 0, 0, 0, 0, 0, 0, 0,
14121416
];
1413-
let version = shred_version::version_from_hash(&Hash::new(&hash));
1417+
let version = shred_version::version_from_hash(&Hash::new_from_array(hash));
14141418
assert_eq!(version, 0xffff);
14151419
let hash = [
14161420
0xa5u8, 0xa5, 0x5a, 0x5a, 0xa5, 0xa5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14171421
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14181422
];
1419-
let version = shred_version::version_from_hash(&Hash::new(&hash));
1423+
let version = shred_version::version_from_hash(&Hash::new_from_array(hash));
14201424
assert_eq!(version, 0x5a5b);
14211425
}
14221426

ledger/src/shred/merkle.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ macro_rules! impl_merkle_shred {
377377
let offset = self.chained_merkle_root_offset()?;
378378
self.payload
379379
.get(offset..offset + SIZE_OF_MERKLE_ROOT)
380-
.map(Hash::new)
380+
.map(|chained_merkle_root| {
381+
<[u8; SIZE_OF_MERKLE_ROOT]>::try_from(chained_merkle_root)
382+
.map(Hash::new_from_array)
383+
.unwrap()
384+
})
381385
.ok_or(Error::InvalidPayloadSize(self.payload.len()))
382386
}
383387

merkle-tree/src/merkle_tree.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl MerkleTree {
179179

180180
#[cfg(test)]
181181
mod tests {
182-
use super::*;
182+
use {super::*, solana_hash::HASH_BYTES};
183183

184184
const TEST: &[&[u8]] = &[
185185
b"my", b"very", b"eager", b"mother", b"just", b"served", b"us", b"nine", b"pizzas",
@@ -209,7 +209,9 @@ mod tests {
209209
// changes
210210
let bytes = hex::decode("b40c847546fdceea166f927fc46c5ca33c3638236a36275c1346d3dffb84e1bc")
211211
.unwrap();
212-
let expected = Hash::new(&bytes);
212+
let expected = <[u8; HASH_BYTES]>::try_from(bytes)
213+
.map(Hash::new_from_array)
214+
.unwrap();
213215
assert_eq!(mt.get_root(), Some(&expected));
214216
}
215217

perf/src/packet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ mod tests {
259259
#[test]
260260
fn test_to_packet_batches() {
261261
let keypair = Keypair::new();
262-
let hash = Hash::new(&[1; 32]);
262+
let hash = Hash::new_from_array([1; 32]);
263263
let tx = system_transaction::transfer(&keypair, &keypair.pubkey(), 1, hash);
264264
let rv = to_packet_batches_for_tests(&[tx.clone(); 1]);
265265
assert_eq!(rv.len(), 1);

programs/bpf_loader/src/syscalls/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ mod tests {
35183518
let mut src_rewards = create_filled_type::<EpochRewards>(false);
35193519
src_rewards.distribution_starting_block_height = 42;
35203520
src_rewards.num_partitions = 2;
3521-
src_rewards.parent_blockhash = Hash::new(&[3; 32]);
3521+
src_rewards.parent_blockhash = Hash::new_from_array([3; 32]);
35223522
src_rewards.total_points = 4;
35233523
src_rewards.total_rewards = 100;
35243524
src_rewards.distributed_rewards = 10;

rpc-client-nonce-utils/src/blockhash_query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ mod tests {
344344
.get_blockhash(&rpc_client, CommitmentConfig::default())
345345
.is_err());
346346

347-
let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[2u8; 32]));
347+
let durable_nonce = DurableNonce::from_blockhash(&Hash::new_from_array([2u8; 32]));
348348
let nonce_blockhash = *durable_nonce.as_hash();
349349
let nonce_fee_calc = FeeCalculator::new(4242);
350350
let data = nonce::state::Data {

rpc-client-nonce-utils/src/nonblocking/blockhash_query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ mod tests {
365365
.await
366366
.is_err());
367367

368-
let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[2u8; 32]));
368+
let durable_nonce = DurableNonce::from_blockhash(&Hash::new_from_array([2u8; 32]));
369369
let nonce_blockhash = *durable_nonce.as_hash();
370370
let nonce_fee_calc = FeeCalculator::new(4242);
371371
let data = nonce::state::Data {

rpc/src/transaction_status_service.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub(crate) mod tests {
347347
let pubkey = Pubkey::new_unique();
348348

349349
let mut nonce_account = nonce_account::create_account(1).into_inner();
350-
let durable_nonce = DurableNonce::from_blockhash(&Hash::new(&[42u8; 32]));
350+
let durable_nonce = DurableNonce::from_blockhash(&Hash::new_from_array([42u8; 32]));
351351
let data = nonce::state::Data::new(Pubkey::from([1u8; 32]), durable_nonce, 42);
352352
nonce_account
353353
.set_state(&nonce::state::Versions::new(nonce::State::Initialized(

runtime/benches/status_cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
solana_accounts_db::ancestors::Ancestors,
88
solana_runtime::{bank::BankStatusCache, status_cache::*},
99
solana_sdk::{
10-
hash::{hash, Hash},
10+
hash::{hash, Hash, HASH_BYTES},
1111
signature::{Signature, SIGNATURE_BYTES},
1212
},
1313
test::Bencher,
@@ -19,7 +19,7 @@ fn bench_status_cache_serialize(bencher: &mut Bencher) {
1919
status_cache.add_root(0);
2020
status_cache.clear();
2121
for hash_index in 0..100 {
22-
let blockhash = Hash::new(&vec![hash_index; std::mem::size_of::<Hash>()]);
22+
let blockhash = Hash::new_from_array([hash_index; HASH_BYTES]);
2323
let mut id = blockhash;
2424
for _ in 0..100 {
2525
id = hash(id.as_ref());

0 commit comments

Comments
 (0)