Clearer updates handling

This commit is contained in:
Tomasz Drwięga
2016-12-09 08:31:58 +00:00
parent 93230dd4c2
commit 8596134c0f
7 changed files with 63 additions and 26 deletions

View File

@@ -105,6 +105,11 @@ impl KeyDirectory for DiskDirectory {
Ok(accounts)
}
fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
// Disk store handles updates correctly iff filename is the same
self.insert(account)
}
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
// transform account into key file
let keyfile: json::KeyFile = account.clone().into();

View File

@@ -88,6 +88,10 @@ impl KeyDirectory for GethDirectory {
self.dir.insert(account)
}
fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.update(account)
}
fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
self.dir.remove(account)
}

View File

@@ -29,6 +29,7 @@ pub enum DirectoryType {
pub trait KeyDirectory: Send + Sync {
fn load(&self) -> Result<Vec<SafeAccount>, Error>;
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error>;
fn remove(&self, account: &SafeAccount) -> Result<(), Error>;
fn path(&self) -> Option<&PathBuf> { None }
}

View File

@@ -67,6 +67,10 @@ impl KeyDirectory for ParityDirectory {
self.dir.insert(account)
}
fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.update(account)
}
fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
self.dir.remove(account)
}

View File

@@ -92,7 +92,7 @@ impl SecretStore for EthStore {
let secret = try!(safe_account.crypto.secret(password).map_err(|_| Error::InvalidPassword));
safe_account.address = try!(KeyPair::from_secret(secret)).address();
let address = safe_account.address.clone();
try!(self.store.save(safe_account));
try!(self.store.import(safe_account));
Ok(address)
}
@@ -129,19 +129,21 @@ impl SecretStore for EthStore {
}
fn set_name(&self, address: &Address, name: String) -> Result<(), Error> {
let mut account = try!(self.get(address));
let old = try!(self.get(address));
let mut account = old.clone();
account.name = name;
// save to file
self.store.save(account)
self.store.update(old, account)
}
fn set_meta(&self, address: &Address, meta: String) -> Result<(), Error> {
let mut account = try!(self.get(address));
let old = try!(self.get(address));
let mut account = old.clone();
account.meta = meta;
// save to file
self.store.save(account)
self.store.update(old, account)
}
fn local_path(&self) -> String {
@@ -213,20 +215,32 @@ impl EthMultiStore {
}
}
fn save(&self, account: SafeAccount) -> Result<(), Error> {
//save to file
fn import(&self, account: SafeAccount) -> Result<(), Error> {
// save to file
let account = try!(self.dir.insert(account));
// update cache
let mut cache = self.cache.write();
let mut accounts = cache.entry(account.address.clone()).or_insert_with(Vec::new);
// TODO [ToDr] That is crappy way of overcoming set_name, set_meta, etc.
// Avoid cloning instead!
accounts.retain(|acc| acc.filename != account.filename);
accounts.push(account);
Ok(())
}
fn update(&self, old: SafeAccount, new: SafeAccount) -> Result<(), Error> {
// save to file
let account = try!(self.dir.update(new));
// update cache
let mut cache = self.cache.write();
let mut accounts = cache.entry(account.address.clone()).or_insert_with(Vec::new);
// Remove old account
accounts.retain(|acc| acc != &old);
// And push updated to the end
accounts.push(account);
Ok(())
}
}
impl SimpleSecretStore for EthMultiStore {
@@ -235,7 +249,7 @@ impl SimpleSecretStore for EthMultiStore {
let id: [u8; 16] = Random::random();
let account = SafeAccount::create(&keypair, id, password, self.iterations, "".to_owned(), "{}".to_owned());
let address = account.address.clone();
try!(self.save(account));
try!(self.import(account));
Ok(address)
}
@@ -278,11 +292,9 @@ impl SimpleSecretStore for EthMultiStore {
fn change_password(&self, address: &Address, old_password: &str, new_password: &str) -> Result<(), Error> {
let accounts = try!(self.get(address));
for account in accounts {
// First remove
try!(self.remove_account(&address, old_password));
// Then insert back with new password
// Change password
let new_account = try!(account.change_password(old_password, new_password, self.iterations));
try!(self.save(new_account));
try!(self.update(account, new_account));
}
Ok(())
}

View File

@@ -63,6 +63,10 @@ impl KeyDirectory for TransientDir {
self.dir.load()
}
fn update(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.update(account)
}
fn insert(&self, account: SafeAccount) -> Result<SafeAccount, Error> {
self.dir.insert(account)
}