Use sha3 for fat key

This commit is contained in:
arkpar 2016-11-27 12:58:14 +01:00
parent e91f7977fb
commit 3aceac60de
8 changed files with 13 additions and 95 deletions

View File

@ -121,10 +121,6 @@ impl<'db> HashDB for AccountDB<'db>{
fn remove(&mut self, _key: &H256) { fn remove(&mut self, _key: &H256) {
unimplemented!() unimplemented!()
} }
fn get_aux(&self, hash: &[u8]) -> Option<DBValue> {
self.db.get_aux(hash)
}
} }
/// DB backend wrapper for Account trie /// DB backend wrapper for Account trie
@ -197,18 +193,6 @@ impl<'db> HashDB for AccountDBMut<'db>{
let key = combine_key(&self.address_hash, key); let key = combine_key(&self.address_hash, key);
self.db.remove(&key) self.db.remove(&key)
} }
fn insert_aux(&mut self, hash: Vec<u8>, value: Vec<u8>) {
self.db.insert_aux(hash, value);
}
fn get_aux(&self, hash: &[u8]) -> Option<DBValue> {
self.db.get_aux(hash)
}
fn remove_aux(&mut self, hash: &[u8]) {
self.db.remove_aux(hash);
}
} }
struct Wrapping<'db>(&'db HashDB); struct Wrapping<'db>(&'db HashDB);

View File

@ -265,6 +265,7 @@ fn execute_import(cmd: ImportBlockchain) -> Result<String, String> {
// save user defaults // save user defaults
user_defaults.pruning = algorithm; user_defaults.pruning = algorithm;
user_defaults.tracing = tracing; user_defaults.tracing = tracing;
user_defaults.fat_db = fat_db;
try!(user_defaults.save(&user_defaults_path)); try!(user_defaults.save(&user_defaults_path));
let report = client.report(); let report = client.report();
@ -393,7 +394,7 @@ fn execute_export_state(cmd: ExportState) -> Result<String, String> {
let at = cmd.at; let at = cmd.at;
let mut i = 0usize; let mut i = 0usize;
out.write_fmt(format_args!("{{ "state\": [", )).expect("Couldn't write to stream."); out.write_fmt(format_args!("{{ \"state\": [", )).expect("Couldn't write to stream.");
loop { loop {
let accounts = try!(client.list_accounts(at, last.as_ref(), 1000).ok_or("Specified block not found")); let accounts = try!(client.list_accounts(at, last.as_ref(), 1000).ok_or("Specified block not found"));
if accounts.is_empty() { if accounts.is_empty() {

View File

@ -257,17 +257,13 @@ pub fn tracing_switch_to_bool(switch: Switch, user_defaults: &UserDefaults) -> R
} }
} }
pub fn fatdb_switch_to_bool(switch: Switch, user_defaults: &UserDefaults, algorithm: Algorithm) -> Result<bool, String> { pub fn fatdb_switch_to_bool(switch: Switch, user_defaults: &UserDefaults, _algorithm: Algorithm) -> Result<bool, String> {
let result = match (user_defaults.is_first_launch, switch, user_defaults.fat_db) { let result = match (user_defaults.is_first_launch, switch, user_defaults.fat_db) {
(false, Switch::On, false) => Err("FatDB resync required".into()), (false, Switch::On, false) => Err("FatDB resync required".into()),
(_, Switch::On, _) => Ok(true), (_, Switch::On, _) => Ok(true),
(_, Switch::Off, _) => Ok(false), (_, Switch::Off, _) => Ok(false),
(_, Switch::Auto, def) => Ok(def), (_, Switch::Auto, def) => Ok(def),
}; };
if result.clone().unwrap_or(false) && algorithm != Algorithm::Archive && algorithm != Algorithm::OverlayRecent {
return Err("Fat DB is not supported with the chosen pruning option. Please rerun with `--pruning=archive`".into());
}
result result
} }

View File

@ -107,21 +107,6 @@ pub trait HashDB: AsHashDB + Send + Sync {
/// } /// }
/// ``` /// ```
fn remove(&mut self, key: &H256); fn remove(&mut self, key: &H256);
/// Insert auxiliary data into hashdb.
fn insert_aux(&mut self, _hash: Vec<u8>, _value: Vec<u8>) {
unimplemented!();
}
/// Get auxiliary data from hashdb.
fn get_aux(&self, _hash: &[u8]) -> Option<DBValue> {
unimplemented!();
}
/// Removes auxiliary data from hashdb.
fn remove_aux(&mut self, _hash: &[u8]) {
unimplemented!();
}
} }
/// Upcast trait. /// Upcast trait.

View File

@ -26,10 +26,6 @@ use kvdb::{Database, DBTransaction};
#[cfg(test)] #[cfg(test)]
use std::env; use std::env;
/// Suffix appended to auxiliary keys to distinguish them from normal keys.
/// Would be nich to use rocksdb columns for this eventually.
const AUX_FLAG: u8 = 255;
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay /// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
/// and latent-removal semantics. /// and latent-removal semantics.
/// ///
@ -108,26 +104,6 @@ impl HashDB for ArchiveDB {
fn remove(&mut self, key: &H256) { fn remove(&mut self, key: &H256) {
self.overlay.remove(key); self.overlay.remove(key);
} }
fn insert_aux(&mut self, hash: Vec<u8>, value: Vec<u8>) {
self.overlay.insert_aux(hash, value);
}
fn get_aux(&self, hash: &[u8]) -> Option<DBValue> {
if let Some(res) = self.overlay.get_aux(hash) {
return Some(res)
}
let mut db_hash = hash.to_vec();
db_hash.push(AUX_FLAG);
self.backing.get(self.column, &db_hash)
.expect("Low-level database error. Some issue with your hard disk?")
}
fn remove_aux(&mut self, hash: &[u8]) {
self.overlay.remove_aux(hash);
}
} }
impl JournalDB for ArchiveDB { impl JournalDB for ArchiveDB {
@ -164,11 +140,6 @@ impl JournalDB for ArchiveDB {
} }
} }
for (mut key, value) in self.overlay.drain_aux() {
key.push(AUX_FLAG);
batch.put(self.column, &key, &value);
}
if self.latest_era.map_or(true, |e| now > e) { if self.latest_era.map_or(true, |e| now > e) {
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now)); batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
self.latest_era = Some(now); self.latest_era = Some(now);
@ -204,11 +175,6 @@ impl JournalDB for ArchiveDB {
} }
} }
for (mut key, value) in self.overlay.drain_aux() {
key.push(AUX_FLAG);
batch.put(self.column, &key, &value);
}
Ok((inserts + deletes) as u32) Ok((inserts + deletes) as u32)
} }

View File

@ -17,7 +17,6 @@
//! Reference-counted memory-based `HashDB` implementation. //! Reference-counted memory-based `HashDB` implementation.
use hash::*; use hash::*;
use bytes::*;
use rlp::*; use rlp::*;
use sha3::*; use sha3::*;
use hashdb::*; use hashdb::*;
@ -72,7 +71,6 @@ use std::collections::hash_map::Entry;
#[derive(Default, Clone, PartialEq)] #[derive(Default, Clone, PartialEq)]
pub struct MemoryDB { pub struct MemoryDB {
data: H256FastMap<(DBValue, i32)>, data: H256FastMap<(DBValue, i32)>,
aux: HashMap<Bytes, DBValue>,
} }
impl MemoryDB { impl MemoryDB {
@ -80,7 +78,6 @@ impl MemoryDB {
pub fn new() -> MemoryDB { pub fn new() -> MemoryDB {
MemoryDB { MemoryDB {
data: H256FastMap::default(), data: H256FastMap::default(),
aux: HashMap::new(),
} }
} }
@ -118,11 +115,6 @@ impl MemoryDB {
mem::replace(&mut self.data, H256FastMap::default()) mem::replace(&mut self.data, H256FastMap::default())
} }
/// Return the internal map of auxiliary data, clearing the current state.
pub fn drain_aux(&mut self) -> HashMap<Bytes, DBValue> {
mem::replace(&mut self.aux, HashMap::new())
}
/// Grab the raw information associated with a key. Returns None if the key /// Grab the raw information associated with a key. Returns None if the key
/// doesn't exist. /// doesn't exist.
/// ///
@ -138,7 +130,6 @@ impl MemoryDB {
/// Returns the size of allocated heap memory /// Returns the size of allocated heap memory
pub fn mem_used(&self) -> usize { pub fn mem_used(&self) -> usize {
self.data.heap_size_of_children() self.data.heap_size_of_children()
+ self.aux.heap_size_of_children()
} }
/// Remove an element and delete it from storage if reference count reaches zero. /// Remove an element and delete it from storage if reference count reaches zero.
@ -256,18 +247,6 @@ impl HashDB for MemoryDB {
self.data.insert(key.clone(), (DBValue::new(), -1)); self.data.insert(key.clone(), (DBValue::new(), -1));
} }
} }
fn insert_aux(&mut self, hash: Vec<u8>, value: Vec<u8>) {
self.aux.insert(hash, DBValue::from_vec(value));
}
fn get_aux(&self, hash: &[u8]) -> Option<DBValue> {
self.aux.get(hash).cloned()
}
fn remove_aux(&mut self, hash: &[u8]) {
self.aux.remove(hash);
}
} }
#[test] #[test]

View File

@ -94,7 +94,8 @@ impl<'db> Iterator for FatDBIterator<'db> {
self.trie_iterator.next() self.trie_iterator.next()
.map(|res| .map(|res|
res.map(|(hash, value)| { res.map(|(hash, value)| {
(self.trie.db().get_aux(&hash).expect("Missing fatdb hash").to_vec(), value) let aux_hash = hash.sha3();
(self.trie.db().get(&aux_hash).expect("Missing fatdb hash").to_vec(), value)
}) })
) )
} }

View File

@ -51,6 +51,10 @@ impl<'db> FatDBMut<'db> {
pub fn db_mut(&mut self) -> &mut HashDB { pub fn db_mut(&mut self) -> &mut HashDB {
self.raw.db_mut() self.raw.db_mut()
} }
fn to_aux_key(key: &[u8]) -> H256 {
key.sha3()
}
} }
impl<'db> TrieMut for FatDBMut<'db> { impl<'db> TrieMut for FatDBMut<'db> {
@ -76,12 +80,14 @@ impl<'db> TrieMut for FatDBMut<'db> {
let hash = key.sha3(); let hash = key.sha3();
try!(self.raw.insert(&hash, value)); try!(self.raw.insert(&hash, value));
let db = self.raw.db_mut(); let db = self.raw.db_mut();
db.insert_aux(hash.to_vec(), key.to_vec()); db.emplace(Self::to_aux_key(&hash), DBValue::from_slice(key));
Ok(()) Ok(())
} }
fn remove(&mut self, key: &[u8]) -> super::Result<()> { fn remove(&mut self, key: &[u8]) -> super::Result<()> {
self.raw.remove(&key.sha3()) let hash = key.sha3();
self.raw.db_mut().remove(&Self::to_aux_key(&hash));
self.raw.remove(&hash)
} }
} }