Use sha3 for fat key
This commit is contained in:
@@ -107,21 +107,6 @@ pub trait HashDB: AsHashDB + Send + Sync {
|
||||
/// }
|
||||
/// ```
|
||||
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.
|
||||
|
||||
@@ -26,10 +26,6 @@ use kvdb::{Database, DBTransaction};
|
||||
#[cfg(test)]
|
||||
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
|
||||
/// and latent-removal semantics.
|
||||
///
|
||||
@@ -108,26 +104,6 @@ impl HashDB for ArchiveDB {
|
||||
fn remove(&mut self, key: &H256) {
|
||||
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 {
|
||||
@@ -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) {
|
||||
batch.put(self.column, &LATEST_ERA_KEY, &encode(&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)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
//! Reference-counted memory-based `HashDB` implementation.
|
||||
|
||||
use hash::*;
|
||||
use bytes::*;
|
||||
use rlp::*;
|
||||
use sha3::*;
|
||||
use hashdb::*;
|
||||
@@ -72,7 +71,6 @@ use std::collections::hash_map::Entry;
|
||||
#[derive(Default, Clone, PartialEq)]
|
||||
pub struct MemoryDB {
|
||||
data: H256FastMap<(DBValue, i32)>,
|
||||
aux: HashMap<Bytes, DBValue>,
|
||||
}
|
||||
|
||||
impl MemoryDB {
|
||||
@@ -80,7 +78,6 @@ impl MemoryDB {
|
||||
pub fn new() -> MemoryDB {
|
||||
MemoryDB {
|
||||
data: H256FastMap::default(),
|
||||
aux: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,11 +115,6 @@ impl MemoryDB {
|
||||
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
|
||||
/// doesn't exist.
|
||||
///
|
||||
@@ -138,7 +130,6 @@ impl MemoryDB {
|
||||
/// Returns the size of allocated heap memory
|
||||
pub fn mem_used(&self) -> usize {
|
||||
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.
|
||||
@@ -256,18 +247,6 @@ impl HashDB for MemoryDB {
|
||||
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]
|
||||
|
||||
@@ -94,7 +94,8 @@ impl<'db> Iterator for FatDBIterator<'db> {
|
||||
self.trie_iterator.next()
|
||||
.map(|res|
|
||||
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)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ impl<'db> FatDBMut<'db> {
|
||||
pub fn db_mut(&mut self) -> &mut HashDB {
|
||||
self.raw.db_mut()
|
||||
}
|
||||
|
||||
fn to_aux_key(key: &[u8]) -> H256 {
|
||||
key.sha3()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'db> TrieMut for FatDBMut<'db> {
|
||||
@@ -76,12 +80,14 @@ impl<'db> TrieMut for FatDBMut<'db> {
|
||||
let hash = key.sha3();
|
||||
try!(self.raw.insert(&hash, value));
|
||||
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(())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user