files list separate fn, sha3 of the list

This commit is contained in:
NikVolf 2017-02-16 18:20:24 +03:00
parent 39d4e46073
commit d4149b965e
2 changed files with 45 additions and 12 deletions

View File

@ -90,11 +90,8 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
}
}
/// all accounts found in keys directory
fn files(&self) -> Result<HashMap<PathBuf, SafeAccount>, Error> {
// it's not done using one iterator cause
// there is an issue with rustc and it takes tooo much time to compile
let paths = fs::read_dir(&self.path)?
fn files(&self) -> Result<Vec<PathBuf>, Error> {
Ok(fs::read_dir(&self.path)?
.flat_map(Result::ok)
.filter(|entry| {
let metadata = entry.metadata().ok();
@ -102,14 +99,21 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
let name = file_name.to_string_lossy();
// filter directories
metadata.map_or(false, |m| !m.is_dir()) &&
// hidden files
!name.starts_with(".") &&
// other ignored files
!IGNORED_FILES.contains(&&*name)
// hidden files
!name.starts_with(".") &&
// other ignored files
!IGNORED_FILES.contains(&&*name)
})
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>();
.collect::<Vec<PathBuf>>()
)
}
/// all accounts found in keys directory
fn files_content(&self) -> Result<HashMap<PathBuf, SafeAccount>, Error> {
// it's not done using one iterator cause
// there is an issue with rustc and it takes tooo much time to compile
let paths = self.files()?;
Ok(paths
.into_iter()
.filter_map(|path| {
@ -166,7 +170,7 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
fn load(&self) -> Result<Vec<SafeAccount>, Error> {
let accounts = self.files()?
let accounts = self.files_content()?
.into_iter()
.map(|(_, account)| account)
.collect();
@ -191,7 +195,7 @@ impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
// enumerate all entries in keystore
// and find entry with given address
let to_remove = self.files()?
let to_remove = self.files_content()?
.into_iter()
.find(|&(_, ref acc)| acc.id == account.id && acc.address == account.address);

View File

@ -68,6 +68,24 @@ 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];
@ -120,4 +138,15 @@ 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());
}
}