Check account hashes in snapshot (#7559)

* Check for incorrect hash value

* Finish up checking for incorrect hash value

* Fix comment a bit

Co-authored-by: sakridge <sakridge@gmail.com>
This commit is contained in:
Ryo Onodera
2019-12-20 09:39:30 +09:00
committed by GitHub
parent 37eaa6e4f9
commit 3c361eb759
5 changed files with 136 additions and 34 deletions

View File

@ -7,9 +7,10 @@ use std::fmt;
use std::mem;
use std::str::FromStr;
pub const HASH_BYTES: usize = 32;
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Hash([u8; 32]);
pub struct Hash([u8; HASH_BYTES]);
#[derive(Clone, Default)]
pub struct Hasher {
@ -28,7 +29,7 @@ impl Hasher {
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; 32]>::try_from(self.hasher.result().as_slice()).unwrap())
Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.result().as_slice()).unwrap())
}
}
@ -73,7 +74,7 @@ impl FromStr for Hash {
impl Hash {
pub fn new(hash_slice: &[u8]) -> Self {
Hash(<[u8; 32]>::try_from(hash_slice).unwrap())
Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
}
}