refactor hashing
This commit is contained in:
parent
513cc6261a
commit
444065e294
@ -22,7 +22,6 @@ use {json, SafeAccount, Error};
|
||||
use json::Uuid;
|
||||
use super::{KeyDirectory, VaultKeyDirectory, VaultKeyDirectoryProvider, VaultKey};
|
||||
use super::vault::{VAULT_FILE_NAME, VaultDiskDirectory};
|
||||
use util::H256;
|
||||
|
||||
const IGNORED_FILES: &'static [&'static str] = &[
|
||||
"thumbs.db",
|
||||
@ -110,11 +109,17 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn files_hash(&self) -> Result<H256, Error> {
|
||||
use util::Hashable;
|
||||
pub fn files_hash(&self) -> Result<u64, Error> {
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::Hasher;
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
let files = self.files()?;
|
||||
let file_strs: Vec<&str> = files.iter().map(|fp| fp.to_str().unwrap_or("")).collect();
|
||||
Ok(file_strs.sha3())
|
||||
for file in files {
|
||||
hasher.write(file.to_str().unwrap_or("").as_bytes())
|
||||
}
|
||||
|
||||
Ok(hasher.finish())
|
||||
}
|
||||
|
||||
/// all accounts found in keys directory
|
||||
@ -220,7 +225,7 @@ impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
|
||||
Some(self)
|
||||
}
|
||||
|
||||
fn hash(&self) -> Result<H256, Error> {
|
||||
fn hash(&self) -> Result<u64, Error> {
|
||||
self.files_hash()
|
||||
}
|
||||
}
|
||||
@ -360,7 +365,7 @@ mod test {
|
||||
let hash = directory.files_hash().expect("Files hash should be calculated ok");
|
||||
assert_eq!(
|
||||
hash,
|
||||
"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".parse().unwrap()
|
||||
15130871412783076140
|
||||
);
|
||||
|
||||
let keypair = Random.generate().unwrap();
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
use std::path::{PathBuf};
|
||||
use {SafeAccount, Error};
|
||||
use util::{H256, FixedHash};
|
||||
|
||||
mod disk;
|
||||
mod geth;
|
||||
@ -64,7 +63,7 @@ pub trait KeyDirectory: Send + Sync {
|
||||
/// Return vault provider, if available
|
||||
fn as_vault_provider(&self) -> Option<&VaultKeyDirectoryProvider> { None }
|
||||
/// Returns hash of the directory content, if supported
|
||||
fn hash(&self) -> Result<H256, Error> { Ok(H256::zero()) }
|
||||
fn hash(&self) -> Result<u64, Error> { Ok(0u64) }
|
||||
}
|
||||
|
||||
/// Vaults provider
|
||||
|
@ -27,7 +27,6 @@ use account::SafeAccount;
|
||||
use presale::PresaleWallet;
|
||||
use json::{self, Uuid};
|
||||
use {import, Error, SimpleSecretStore, SecretStore, SecretVaultRef, StoreAccountRef, Derivation};
|
||||
use util::H256;
|
||||
|
||||
pub struct EthStore {
|
||||
store: EthMultiStore,
|
||||
@ -231,7 +230,7 @@ pub struct EthMultiStore {
|
||||
// order lock: cache, then vaults
|
||||
cache: RwLock<BTreeMap<StoreAccountRef, Vec<SafeAccount>>>,
|
||||
vaults: Mutex<HashMap<String, Box<VaultKeyDirectory>>>,
|
||||
dir_hash: RwLock<Option<H256>>,
|
||||
dir_hash: Mutex<Option<u64>>,
|
||||
}
|
||||
|
||||
impl EthMultiStore {
|
||||
@ -253,7 +252,7 @@ impl EthMultiStore {
|
||||
}
|
||||
|
||||
fn reload_if_changed(&self) -> Result<(), Error> {
|
||||
let mut last_dir_hash = self.dir_hash.write();
|
||||
let mut last_dir_hash = self.dir_hash.lock();
|
||||
let dir_hash = Some(self.dir.hash()?);
|
||||
if *last_dir_hash == dir_hash {
|
||||
return Ok(())
|
||||
|
@ -68,24 +68,6 @@ impl<T> Hashable for T where T: AsRef<[u8]> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Hashable for [T] where T: Hashable {
|
||||
fn sha3(&self) -> H256 {
|
||||
use std::ops::BitXor;
|
||||
|
||||
let mut sha3 = SHA3_EMPTY;
|
||||
for t in self.iter() {
|
||||
sha3 = sha3.bitxor(t.sha3());
|
||||
};
|
||||
|
||||
sha3
|
||||
}
|
||||
// todo: optimize?
|
||||
fn sha3_into(&self, dest: &mut [u8]) {
|
||||
let sha3 = self.sha3();
|
||||
dest.copy_from_slice(&*sha3);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate SHA3 of given stream.
|
||||
pub fn sha3(r: &mut io::BufRead) -> Result<H256, io::Error> {
|
||||
let mut output = [0u8; 32];
|
||||
@ -138,15 +120,4 @@ mod tests {
|
||||
// then
|
||||
assert_eq!(format!("{:?}", hash), "68371d7e884c168ae2022c82bd837d51837718a7f7dfb7aa3f753074a35e1d87");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_sha3_strs() {
|
||||
let strs = vec!["abc".to_owned(), "gdc".to_owned()];
|
||||
let hash = strs.sha3();
|
||||
assert_eq!(hash, "b8f24d705171c55892a34c7b863c258f4d47e6864f7a7da45f84155597a3b338".parse().unwrap());
|
||||
|
||||
let strs = vec!["abc".to_owned(), "gdc_".to_owned()];
|
||||
let hash = strs.sha3();
|
||||
assert_eq!(hash, "41bd661b8e02faccad55cdbb28db974dd5c9ae41825b89907fcf25db793b8b09".parse().unwrap());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user