Trie takes a reference to HashDB.

This commit is contained in:
Gav Wood 2015-12-13 19:36:49 +01:00
parent 6bc56ad004
commit d94c55133b

View File

@ -299,8 +299,8 @@ impl <'a>Node<'a> {
/// assert!(t.db_items_remaining().is_empty()); /// assert!(t.db_items_remaining().is_empty());
/// } /// }
/// ``` /// ```
pub struct TrieDB<'db, T> where T: 'db + HashDB { pub struct TrieDB<'db> {
db: &'db mut T, db: &'db mut HashDB,
root: &'db mut H256, root: &'db mut H256,
pub hash_count: usize, pub hash_count: usize,
} }
@ -311,11 +311,11 @@ enum MaybeChanged<'a> {
Changed(Bytes), Changed(Bytes),
} }
impl<'db, T> TrieDB<'db, T> where T: 'db + HashDB { impl<'db> TrieDB<'db> {
/// Create a new trie with the backing database `db` and empty `root` /// Create a new trie with the backing database `db` and empty `root`
/// Initialise to the state entailed by the genesis block. /// Initialise to the state entailed by the genesis block.
/// This guarantees the trie is built correctly. /// This guarantees the trie is built correctly.
pub fn new(db: &'db mut T, root: &'db mut H256) -> Self { pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self {
let mut r = TrieDB{ let mut r = TrieDB{
db: db, db: db,
root: root, root: root,
@ -329,7 +329,7 @@ impl<'db, T> TrieDB<'db, T> where T: 'db + HashDB {
/// Create a new trie with the backing database `db` and `root` /// Create a new trie with the backing database `db` and `root`
/// Panics, if `root` does not exist /// Panics, if `root` does not exist
pub fn new_existing(db: &'db mut T, root: &'db mut H256) -> Self { pub fn new_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
assert!(db.exists(root)); assert!(db.exists(root));
TrieDB { TrieDB {
db: db, db: db,
@ -339,7 +339,7 @@ impl<'db, T> TrieDB<'db, T> where T: 'db + HashDB {
} }
/// Get the backing database. /// Get the backing database.
pub fn db(&'db self) -> &'db T { pub fn db(&'db self) -> &'db HashDB {
self.db self.db
} }
@ -364,7 +364,7 @@ impl<'db, T> TrieDB<'db, T> where T: 'db + HashDB {
/// Determine occurances of items in the backing database which are not related to this /// Determine occurances of items in the backing database which are not related to this
/// trie. /// trie.
pub fn db_items_remaining(&self) -> HashMap<H256, i32> { pub fn db_items_remaining(&self) -> HashMap<H256, i32> {
let mut ret = self.db().keys(); let mut ret = self.db.keys();
for (k, v) in Self::to_map(self.keys()).into_iter() { for (k, v) in Self::to_map(self.keys()).into_iter() {
let keycount = *ret.get(&k).unwrap_or(&0); let keycount = *ret.get(&k).unwrap_or(&0);
match keycount == v as i32 { match keycount == v as i32 {
@ -892,7 +892,7 @@ impl<'db, T> TrieDB<'db, T> where T: 'db + HashDB {
} }
} }
impl<'db, T> Trie for TrieDB<'db, T> where T: 'db + HashDB { impl<'db> Trie for TrieDB<'db> {
fn root(&self) -> &H256 { &self.root } fn root(&self) -> &H256 { &self.root }
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {
@ -915,7 +915,7 @@ impl<'db, T> Trie for TrieDB<'db, T> where T: 'db + HashDB {
} }
} }
impl<'db, T> fmt::Debug for TrieDB<'db, T> where T: 'db + HashDB { impl<'db> fmt::Debug for TrieDB<'db> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(writeln!(f, "c={:?} [", self.hash_count)); try!(writeln!(f, "c={:?} [", self.hash_count));
let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!"); let root_rlp = self.db.lookup(&self.root).expect("Trie root not found!");
@ -960,7 +960,7 @@ mod tests {
} }
} }
fn populate_trie<'db, T>(db: &'db mut T, root: &'db mut H256, v: &Vec<(Vec<u8>, Vec<u8>)>) -> TrieDB<'db, T> where T: 'db + HashDB { fn populate_trie<'db>(db: &'db mut HashDB, root: &'db mut H256, v: &Vec<(Vec<u8>, Vec<u8>)>) -> TrieDB<'db> {
let mut t = TrieDB::new(db, root); let mut t = TrieDB::new(db, root);
for i in 0..v.len() { for i in 0..v.len() {
let key: &[u8]= &v[i].0; let key: &[u8]= &v[i].0;
@ -970,7 +970,7 @@ mod tests {
t t
} }
fn unpopulate_trie<'a, 'db, T>(t: &mut TrieDB<'db, T>, v: &Vec<(Vec<u8>, Vec<u8>)>) where T: 'db + HashDB { fn unpopulate_trie<'a, 'db>(t: &mut TrieDB<'db>, v: &Vec<(Vec<u8>, Vec<u8>)>) {
for i in v.iter() { for i in v.iter() {
let key: &[u8]= &i.0; let key: &[u8]= &i.0;
t.remove(&key); t.remove(&key);