hash: Use `finalize().into()` instead of `try_into()` for hash results (#35300)

`sha2` and `sha3` crates already moved to `generic-array` 0.14.7,
which means that we can safely convert the hash result to a sized
array just by calling `finalize().into()`, which doesn't return
any errors.
This commit is contained in:
Michal Rostecki 2024-02-24 02:31:32 +00:00 committed by GitHub
parent 9f581113bd
commit e74d5ccca3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 24 additions and 22 deletions

15
Cargo.lock generated
View File

@ -2888,9 +2888,12 @@ dependencies = [
[[package]]
name = "keccak"
version = "0.1.0"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7"
checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654"
dependencies = [
"cpufeatures",
]
[[package]]
name = "kernel32-sys"
@ -4985,9 +4988,9 @@ dependencies = [
[[package]]
name = "sha3"
version = "0.10.4"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaedf34ed289ea47c2b741bb72e5357a209512d67bcd4bda44359e5bf0470f56"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest 0.10.7",
"keccak",
@ -6611,7 +6614,7 @@ dependencies = [
"serde_derive",
"serde_json",
"sha2 0.10.8",
"sha3 0.10.4",
"sha3 0.10.8",
"solana-frozen-abi",
"solana-frozen-abi-macro",
"solana-logger",
@ -7055,7 +7058,7 @@ dependencies = [
"serde_json",
"serde_with",
"sha2 0.10.8",
"sha3 0.10.4",
"sha3 0.10.8",
"siphasher",
"solana-frozen-abi",
"solana-frozen-abi-macro",

View File

@ -301,7 +301,7 @@ serde_with = { version = "2.3.3", default-features = false }
serde_yaml = "0.9.32"
serial_test = "2.0.0"
sha2 = "0.10.8"
sha3 = "0.10.4"
sha3 = "0.10.8"
signal-hook = "0.3.17"
siphasher = "0.3.11"
smallvec = "1.13.1"

View File

@ -1,6 +1,6 @@
use {
sha2::{Digest, Sha256},
std::{convert::TryFrom, fmt},
std::fmt,
};
const HASH_BYTES: usize = 32;
@ -17,9 +17,7 @@ impl Hasher {
self.hasher.update(val);
}
pub fn result(self) -> Hash {
// At the time of this writing, the sha2 library is stuck on an old version
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.finalize().as_slice()).unwrap())
Hash(self.hasher.finalize().into())
}
}

View File

@ -2490,9 +2490,12 @@ dependencies = [
[[package]]
name = "keccak"
version = "0.1.0"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7"
checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654"
dependencies = [
"cpufeatures",
]
[[package]]
name = "kernel32-sys"
@ -4409,9 +4412,9 @@ dependencies = [
[[package]]
name = "sha3"
version = "0.10.4"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaedf34ed289ea47c2b741bb72e5357a209512d67bcd4bda44359e5bf0470f56"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest 0.10.7",
"keccak",
@ -5381,7 +5384,7 @@ dependencies = [
"serde_derive",
"serde_json",
"sha2 0.10.8",
"sha3 0.10.4",
"sha3 0.10.8",
"solana-frozen-abi",
"solana-frozen-abi-macro",
"solana-sdk-macro",
@ -6169,7 +6172,7 @@ dependencies = [
"serde_json",
"serde_with",
"sha2 0.10.8",
"sha3 0.10.4",
"sha3 0.10.8",
"siphasher",
"solana-frozen-abi",
"solana-frozen-abi-macro",

View File

@ -48,9 +48,7 @@ impl Hasher {
}
}
pub fn result(self) -> Hash {
// At the time of this writing, the sha3 library is stuck on an old version
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.finalize().as_slice()).unwrap())
Hash(self.hasher.finalize().into())
}
}

View File

@ -481,7 +481,7 @@ impl Message {
let mut hasher = blake3::Hasher::new();
hasher.update(b"solana-tx-message-v1");
hasher.update(message_bytes);
Hash(<[u8; crate::hash::HASH_BYTES]>::try_from(hasher.finalize().as_slice()).unwrap())
Hash(hasher.finalize().into())
}
pub fn compile_instruction(&self, ix: &Instruction) -> CompiledInstruction {

View File

@ -148,7 +148,7 @@ impl VersionedMessage {
let mut hasher = blake3::Hasher::new();
hasher.update(b"solana-tx-message-v1");
hasher.update(message_bytes);
Hash(<[u8; crate::hash::HASH_BYTES]>::try_from(hasher.finalize().as_slice()).unwrap())
Hash(hasher.finalize().into())
}
}