2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-11-22 11:56:27 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-11-22 11:56:27 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-11-22 11:56:27 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-22 11:56:27 +01:00
|
|
|
|
2018-08-07 14:52:23 +02:00
|
|
|
//! Address Book Store
|
2016-11-22 11:56:27 +01:00
|
|
|
|
|
|
|
use std::{fs, fmt, hash, ops};
|
2017-01-30 10:59:46 +01:00
|
|
|
use std::collections::HashMap;
|
2017-02-03 13:56:48 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-11-22 11:56:27 +01:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
use ethkey::Address;
|
|
|
|
use log::{trace, warn};
|
|
|
|
|
|
|
|
use crate::AccountMeta;
|
2016-11-22 11:56:27 +01:00
|
|
|
|
|
|
|
/// Disk-backed map from Address to String. Uses JSON.
|
|
|
|
pub struct AddressBook {
|
|
|
|
cache: DiskMap<Address, AccountMeta>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddressBook {
|
|
|
|
/// Creates new address book at given directory.
|
2017-02-03 13:56:48 +01:00
|
|
|
pub fn new(path: &Path) -> Self {
|
2016-11-22 11:56:27 +01:00
|
|
|
let mut r = AddressBook {
|
2017-02-03 13:56:48 +01:00
|
|
|
cache: DiskMap::new(path, "address_book.json")
|
2016-11-22 11:56:27 +01:00
|
|
|
};
|
2017-01-30 10:59:46 +01:00
|
|
|
r.cache.revert(AccountMeta::read);
|
2016-11-22 11:56:27 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates transient address book (no changes are saved to disk).
|
|
|
|
pub fn transient() -> Self {
|
|
|
|
AddressBook {
|
|
|
|
cache: DiskMap::transient()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the address book.
|
|
|
|
pub fn get(&self) -> HashMap<Address, AccountMeta> {
|
|
|
|
self.cache.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save(&self) {
|
2017-01-30 10:59:46 +01:00
|
|
|
self.cache.save(AccountMeta::write)
|
2016-11-22 11:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets new name for given address.
|
|
|
|
pub fn set_name(&mut self, a: Address, name: String) {
|
|
|
|
{
|
2017-10-15 15:10:20 +02:00
|
|
|
let x = self.cache.entry(a)
|
2016-11-22 11:56:27 +01:00
|
|
|
.or_insert_with(|| AccountMeta {name: Default::default(), meta: "{}".to_owned(), uuid: None});
|
|
|
|
x.name = name;
|
|
|
|
}
|
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets new meta for given address.
|
|
|
|
pub fn set_meta(&mut self, a: Address, meta: String) {
|
|
|
|
{
|
2017-10-15 15:10:20 +02:00
|
|
|
let x = self.cache.entry(a)
|
2016-11-22 11:56:27 +01:00
|
|
|
.or_insert_with(|| AccountMeta {name: "Anonymous".to_owned(), meta: Default::default(), uuid: None});
|
|
|
|
x.meta = meta;
|
|
|
|
}
|
|
|
|
self.save();
|
|
|
|
}
|
2016-12-07 16:53:46 +01:00
|
|
|
|
|
|
|
/// Removes an entry
|
|
|
|
pub fn remove(&mut self, a: Address) {
|
2016-12-07 17:07:28 +01:00
|
|
|
self.cache.remove(&a);
|
2016-12-07 16:53:46 +01:00
|
|
|
self.save();
|
|
|
|
}
|
2016-11-22 11:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Disk-serializable HashMap
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct DiskMap<K: hash::Hash + Eq, V> {
|
|
|
|
path: PathBuf,
|
|
|
|
cache: HashMap<K, V>,
|
|
|
|
transient: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: hash::Hash + Eq, V> ops::Deref for DiskMap<K, V> {
|
|
|
|
type Target = HashMap<K, V>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: hash::Hash + Eq, V> ops::DerefMut for DiskMap<K, V> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: hash::Hash + Eq, V> DiskMap<K, V> {
|
2017-02-03 13:56:48 +01:00
|
|
|
pub fn new(path: &Path, file_name: &str) -> Self {
|
|
|
|
let mut path = path.to_owned();
|
2016-11-22 11:56:27 +01:00
|
|
|
path.push(file_name);
|
|
|
|
trace!(target: "diskmap", "path={:?}", path);
|
|
|
|
DiskMap {
|
|
|
|
path: path,
|
|
|
|
cache: HashMap::new(),
|
|
|
|
transient: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transient() -> Self {
|
2017-02-03 13:56:48 +01:00
|
|
|
let mut map = DiskMap::new(&PathBuf::new(), "diskmap.json".into());
|
2016-11-22 11:56:27 +01:00
|
|
|
map.transient = true;
|
|
|
|
map
|
|
|
|
}
|
|
|
|
|
|
|
|
fn revert<F, E>(&mut self, read: F) where
|
|
|
|
F: Fn(fs::File) -> Result<HashMap<K, V>, E>,
|
|
|
|
E: fmt::Display,
|
|
|
|
{
|
|
|
|
if self.transient { return; }
|
|
|
|
trace!(target: "diskmap", "revert {:?}", self.path);
|
|
|
|
let _ = fs::File::open(self.path.clone())
|
|
|
|
.map_err(|e| trace!(target: "diskmap", "Couldn't open disk map: {}", e))
|
|
|
|
.and_then(|f| read(f).map_err(|e| warn!(target: "diskmap", "Couldn't read disk map: {}", e)))
|
|
|
|
.and_then(|m| {
|
|
|
|
self.cache = m;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save<F, E>(&self, write: F) where
|
|
|
|
F: Fn(&HashMap<K, V>, &mut fs::File) -> Result<(), E>,
|
|
|
|
E: fmt::Display,
|
|
|
|
{
|
|
|
|
if self.transient { return; }
|
|
|
|
trace!(target: "diskmap", "save {:?}", self.path);
|
|
|
|
let _ = fs::File::create(self.path.clone())
|
|
|
|
.map_err(|e| warn!(target: "diskmap", "Couldn't open disk map for writing: {}", e))
|
|
|
|
.and_then(|mut f| {
|
|
|
|
write(&self.cache, &mut f).map_err(|e| warn!(target: "diskmap", "Couldn't write to disk map: {}", e))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-08-07 14:52:23 +02:00
|
|
|
use super::AddressBook;
|
2016-11-22 11:56:27 +01:00
|
|
|
use std::collections::HashMap;
|
2018-01-19 17:32:53 +01:00
|
|
|
use tempdir::TempDir;
|
2019-02-07 14:34:24 +01:00
|
|
|
use crate::account_data::AccountMeta;
|
2016-11-22 11:56:27 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_save_and_reload_address_book() {
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let mut b = AddressBook::new(tempdir.path());
|
2016-11-22 11:56:27 +01:00
|
|
|
b.set_name(1.into(), "One".to_owned());
|
|
|
|
b.set_meta(1.into(), "{1:1}".to_owned());
|
2018-01-19 17:32:53 +01:00
|
|
|
let b = AddressBook::new(tempdir.path());
|
2019-02-07 14:34:24 +01:00
|
|
|
assert_eq!(b.get(), vec![
|
|
|
|
(1, AccountMeta {name: "One".to_owned(), meta: "{1:1}".to_owned(), uuid: None})
|
|
|
|
].into_iter().map(|(a, b)| (a.into(), b)).collect::<HashMap<_, _>>());
|
2016-11-22 11:56:27 +01:00
|
|
|
}
|
|
|
|
|
2016-12-10 12:34:20 +01:00
|
|
|
#[test]
|
|
|
|
fn should_remove_address() {
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let mut b = AddressBook::new(tempdir.path());
|
2016-12-10 12:34:20 +01:00
|
|
|
|
|
|
|
b.set_name(1.into(), "One".to_owned());
|
|
|
|
b.set_name(2.into(), "Two".to_owned());
|
|
|
|
b.set_name(3.into(), "Three".to_owned());
|
|
|
|
b.remove(2.into());
|
|
|
|
|
2018-01-19 17:32:53 +01:00
|
|
|
let b = AddressBook::new(tempdir.path());
|
2019-02-07 14:34:24 +01:00
|
|
|
assert_eq!(b.get(), vec![
|
|
|
|
(1, AccountMeta{name: "One".to_owned(), meta: "{}".to_owned(), uuid: None}),
|
|
|
|
(3, AccountMeta{name: "Three".to_owned(), meta: "{}".to_owned(), uuid: None}),
|
|
|
|
].into_iter().map(|(a, b)| (a.into(), b)).collect::<HashMap<_, _>>());
|
2016-12-10 12:34:20 +01:00
|
|
|
}
|
2016-11-22 11:56:27 +01:00
|
|
|
}
|