plug to store

This commit is contained in:
NikVolf 2017-02-16 22:10:29 +03:00
parent 43ce5bef7e
commit 513cc6261a
3 changed files with 24 additions and 3 deletions

View File

@ -219,6 +219,10 @@ impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
fn as_vault_provider(&self) -> Option<&VaultKeyDirectoryProvider> {
Some(self)
}
fn hash(&self) -> Result<H256, Error> {
self.files_hash()
}
}
impl<T> VaultKeyDirectoryProvider for DiskDirectory<T> where T: KeyFileManager {

View File

@ -16,6 +16,7 @@
use std::path::{PathBuf};
use {SafeAccount, Error};
use util::{H256, FixedHash};
mod disk;
mod geth;
@ -62,6 +63,8 @@ pub trait KeyDirectory: Send + Sync {
fn path(&self) -> Option<&PathBuf> { None }
/// 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()) }
}
/// Vaults provider

View File

@ -27,6 +27,7 @@ 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,
@ -230,6 +231,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>>,
}
impl EthMultiStore {
@ -244,11 +246,23 @@ impl EthMultiStore {
vaults: Mutex::new(HashMap::new()),
iterations: iterations,
cache: Default::default(),
dir_hash: Default::default(),
};
store.reload_accounts()?;
Ok(store)
}
fn reload_if_changed(&self) -> Result<(), Error> {
let mut last_dir_hash = self.dir_hash.write();
let dir_hash = Some(self.dir.hash()?);
if *last_dir_hash == dir_hash {
return Ok(())
}
self.reload_accounts()?;
*last_dir_hash = dir_hash;
Ok(())
}
fn reload_accounts(&self) -> Result<(), Error> {
let mut cache = self.cache.write();
@ -284,7 +298,7 @@ impl EthMultiStore {
}
}
self.reload_accounts()?;
self.reload_if_changed()?;
let cache = self.cache.read();
let accounts = cache.get(account).ok_or(Error::InvalidAccount)?;
if accounts.is_empty() {
@ -431,7 +445,7 @@ impl SimpleSecretStore for EthMultiStore {
}
fn account_ref(&self, address: &Address) -> Result<StoreAccountRef, Error> {
self.reload_accounts()?;
self.reload_if_changed()?;
self.cache.read().keys()
.find(|r| &r.address == address)
.cloned()
@ -439,7 +453,7 @@ impl SimpleSecretStore for EthMultiStore {
}
fn accounts(&self) -> Result<Vec<StoreAccountRef>, Error> {
self.reload_accounts()?;
self.reload_if_changed()?;
Ok(self.cache.read().keys().cloned().collect())
}