trie factory in progress

This commit is contained in:
debris 2016-06-27 10:59:59 +02:00
parent f3a6da7c1e
commit 5ecbeaa82f
6 changed files with 190 additions and 34 deletions

View File

@ -17,28 +17,25 @@
use hash::H256; use hash::H256;
use sha3::Hashable; use sha3::Hashable;
use hashdb::HashDB; use hashdb::HashDB;
use super::{TrieDBMut, Trie, TrieDB, TrieMut, TrieDBIterator, TrieError}; use super::{TrieDB, Trie, TrieDBIterator, TrieError};
/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database. /// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
/// ///
/// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing `TrieDBMut` object. /// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing `TrieDB` object.
pub struct FatDB<'db> { pub struct FatDB<'db> {
raw: TrieDBMut<'db>, raw: TrieDB<'db>,
} }
impl<'db> FatDB<'db> { impl<'db> FatDB<'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 HashDB, root: &'db mut H256) -> Self { pub fn new(db: &'db HashDB, root: &'db H256) -> Result<Self, TrieError> {
FatDB { raw: TrieDBMut::new(db, root) } let fatdb = FatDB {
} raw: try!(TrieDB::new(db, root))
};
/// Create a new trie with the backing database `db` and `root`. Ok(fatdb)
///
/// Returns an error if root does not exist.
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Result<Self, TrieError> {
Ok(FatDB { raw: try!(TrieDBMut::from_existing(db, root)) })
} }
/// Get the backing database. /// Get the backing database.
@ -46,13 +43,17 @@ impl<'db> FatDB<'db> {
self.raw.db() self.raw.db()
} }
/// Get the backing database. /// Iterator over all key / vlaues in the trie.
pub fn db_mut(&mut self) -> &mut HashDB { pub fn iter(&self) -> FatDBIterator {
self.raw.db_mut() FatDBIterator::new(&self.raw)
} }
} }
impl<'db> Trie for FatDB<'db> { impl<'db> Trie for FatDB<'db> {
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a> {
Box::new(FatDB::iter(self))
}
fn root(&self) -> &H256 { fn root(&self) -> &H256 {
self.raw.root() self.raw.root()
} }
@ -66,19 +67,6 @@ impl<'db> Trie for FatDB<'db> {
} }
} }
impl<'db> TrieMut for FatDB<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) {
let hash = key.sha3();
self.raw.insert(&hash, value);
let db = self.raw.db_mut();
db.insert_aux(hash.to_vec(), key.to_vec());
}
fn remove(&mut self, key: &[u8]) {
self.raw.remove(&key.sha3());
}
}
/// Itarator over inserted pairs of key values. /// Itarator over inserted pairs of key values.
pub struct FatDBIterator<'db> { pub struct FatDBIterator<'db> {
trie_iterator: TrieDBIterator<'db>, trie_iterator: TrieDBIterator<'db>,
@ -86,6 +74,7 @@ pub struct FatDBIterator<'db> {
} }
impl<'db> FatDBIterator<'db> { impl<'db> FatDBIterator<'db> {
/// Creates new iterator.
pub fn new(trie: &'db TrieDB) -> Self { pub fn new(trie: &'db TrieDB) -> Self {
FatDBIterator { FatDBIterator {
trie_iterator: TrieDBIterator::new(trie), trie_iterator: TrieDBIterator::new(trie),
@ -108,15 +97,15 @@ impl<'db> Iterator for FatDBIterator<'db> {
#[test] #[test]
fn fatdb_to_trie() { fn fatdb_to_trie() {
use memorydb::MemoryDB; use memorydb::MemoryDB;
use super::TrieDB; use trie::{FatDBMut, TrieMut};
let mut memdb = MemoryDB::new(); let mut memdb = MemoryDB::new();
let mut root = H256::default(); let mut root = H256::default();
{ {
let mut t = FatDB::new(&mut memdb, &mut root); let mut t = FatDBMut::new(&mut memdb, &mut root);
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
} }
let t = TrieDB::new(&memdb, &root).unwrap(); let t = FatDB::new(&memdb, &root).unwrap();
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap(), &[0x01u8, 0x23]); assert_eq!(t.get(&[0x01u8, 0x23]).unwrap(), &[0x01u8, 0x23]);
assert_eq!(FatDBIterator::new(&t).collect::<Vec<_>>(), vec![(vec![0x01u8, 0x23], &[0x01u8, 0x23] as &[u8])]); assert_eq!(t.iter().collect::<Vec<_>>(), vec![(vec![0x01u8, 0x23], &[0x01u8, 0x23] as &[u8])]);
} }

95
util/src/trie/fatdbmut.rs Normal file
View File

@ -0,0 +1,95 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// 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.
// Parity is distributed in the hope that it will be useful,
// 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
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use hash::H256;
use sha3::Hashable;
use hashdb::HashDB;
use super::{TrieDBMut, Trie, TrieMut, TrieError};
/// A mutable `Trie` implementation which hashes keys and uses a generic `HashDB` backing database.
///
/// Use it as a `Trie` or `TrieMut` trait object. You can use `raw()` to get the backing `TrieDBMut` object.
pub struct FatDBMut<'db> {
raw: TrieDBMut<'db>,
}
impl<'db> FatDBMut<'db> {
/// Create a new trie with the backing database `db` and empty `root`
/// Initialise to the state entailed by the genesis block.
/// This guarantees the trie is built correctly.
pub fn new(db: &'db mut HashDB, root: &'db mut H256) -> Self {
FatDBMut { raw: TrieDBMut::new(db, root) }
}
/// Create a new trie with the backing database `db` and `root`.
///
/// Returns an error if root does not exist.
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Result<Self, TrieError> {
Ok(FatDBMut { raw: try!(TrieDBMut::from_existing(db, root)) })
}
/// Get the backing database.
pub fn db(&self) -> &HashDB {
self.raw.db()
}
/// Get the backing database.
pub fn db_mut(&mut self) -> &mut HashDB {
self.raw.db_mut()
}
}
impl<'db> Trie for FatDBMut<'db> {
fn root(&self) -> &H256 {
self.raw.root()
}
fn contains(&self, key: &[u8]) -> bool {
self.raw.contains(&key.sha3())
}
fn get<'a, 'key>(&'a self, key: &'key [u8]) -> Option<&'a [u8]> where 'a: 'key {
self.raw.get(&key.sha3())
}
}
impl<'db> TrieMut for FatDBMut<'db> {
fn insert(&mut self, key: &[u8], value: &[u8]) {
let hash = key.sha3();
self.raw.insert(&hash, value);
let db = self.raw.db_mut();
db.insert_aux(hash.to_vec(), key.to_vec());
}
fn remove(&mut self, key: &[u8]) {
self.raw.remove(&key.sha3());
}
}
#[test]
fn fatdb_to_trie() {
use memorydb::MemoryDB;
use super::TrieDB;
let mut memdb = MemoryDB::new();
let mut root = H256::default();
{
let mut t = FatDBMut::new(&mut memdb, &mut root);
t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]);
}
let t = TrieDB::new(&memdb, &root).unwrap();
assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap(), &[0x01u8, 0x23]);
}

View File

@ -17,6 +17,8 @@
//! Trie interface and implementation. //! Trie interface and implementation.
use std::fmt; use std::fmt;
use hash::H256;
use hashdb::HashDB;
/// Export the trietraits module. /// Export the trietraits module.
pub mod trietraits; pub mod trietraits;
@ -37,6 +39,8 @@ pub mod sectriedbmut;
mod fatdb; mod fatdb;
mod fatdbmut;
pub use self::trietraits::{Trie, TrieMut}; pub use self::trietraits::{Trie, TrieMut};
pub use self::standardmap::{Alphabet, StandardMap, ValueMode}; pub use self::standardmap::{Alphabet, StandardMap, ValueMode};
pub use self::triedbmut::TrieDBMut; pub use self::triedbmut::TrieDBMut;
@ -44,6 +48,7 @@ pub use self::triedb::{TrieDB, TrieDBIterator};
pub use self::sectriedbmut::SecTrieDBMut; pub use self::sectriedbmut::SecTrieDBMut;
pub use self::sectriedb::SecTrieDB; pub use self::sectriedb::SecTrieDB;
pub use self::fatdb::{FatDB, FatDBIterator}; pub use self::fatdb::{FatDB, FatDBIterator};
pub use self::fatdbmut::FatDBMut;
/// Trie Errors /// Trie Errors
#[derive(Debug)] #[derive(Debug)]
@ -57,3 +62,57 @@ impl fmt::Display for TrieError {
write!(f, "Trie Error: Invalid state root.") write!(f, "Trie Error: Invalid state root.")
} }
} }
/// Trie types
#[derive(Debug, Clone)]
pub enum TrieSpec {
/// Secure trie.
Secure,
/// Secure trie with fat database.
Fat,
}
impl Default for TrieSpec {
fn default() -> TrieSpec {
TrieSpec::Secure
}
}
/// Trie factory.
#[derive(Default, Clone)]
pub struct TrieFactory {
spec: TrieSpec,
}
impl TrieFactory {
/// Creates new factory.
pub fn new(spec: TrieSpec) -> Self {
TrieFactory {
spec: spec,
}
}
/// Create new immutable instance of Trie.
pub fn create<'db>(&self, db: &'db HashDB, root: &'db H256) -> Result<Box<Trie + 'db>, TrieError> {
match self.spec {
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDB::new(db, root)))),
TrieSpec::Fat => Ok(Box::new(try!(FatDB::new(db, root)))),
}
}
/// Create new mutable instance of Trie.
pub fn create_mut<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<Trie + 'db>, TrieError> {
match self.spec {
TrieSpec::Secure => Ok(Box::new(SecTrieDBMut::new(db, root))),
TrieSpec::Fat => Ok(Box::new(FatDBMut::new(db, root))),
}
}
/// Create new mutable instance of trie and check for errors.
pub fn from_existing<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<Trie + 'db>, TrieError> {
match self.spec {
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDBMut::from_existing(db, root)))),
TrieSpec::Fat => Ok(Box::new(try!(FatDBMut::from_existing(db, root)))),
}
}
}

View File

@ -50,6 +50,10 @@ impl<'db> SecTrieDB<'db> {
} }
impl<'db> Trie for SecTrieDB<'db> { impl<'db> Trie for SecTrieDB<'db> {
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a> {
Box::new(TrieDB::iter(&self.raw))
}
fn root(&self) -> &H256 { self.raw.root() } fn root(&self) -> &H256 { self.raw.root() }
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {

View File

@ -18,7 +18,7 @@ use common::*;
use hashdb::*; use hashdb::*;
use nibbleslice::*; use nibbleslice::*;
use rlp::*; use rlp::*;
use super::trietraits::Trie; use super::trietraits::{Trie};
use super::node::Node; use super::node::Node;
use super::TrieError; use super::TrieError;
@ -331,10 +331,16 @@ impl<'a> Iterator for TrieDBIterator<'a> {
impl<'db> TrieDB<'db> { impl<'db> TrieDB<'db> {
/// Get all keys/values stored in the trie. /// Get all keys/values stored in the trie.
pub fn iter(&self) -> TrieDBIterator { TrieDBIterator::new(self) } pub fn iter(&self) -> TrieDBIterator {
TrieDBIterator::new(self)
}
} }
impl<'db> Trie for TrieDB<'db> { impl<'db> Trie for TrieDB<'db> {
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a> {
Box::new(TrieDB::iter(self))
}
fn root(&self) -> &H256 { &self.root } fn root(&self) -> &H256 { &self.root }
fn contains(&self, key: &[u8]) -> bool { fn contains(&self, key: &[u8]) -> bool {

View File

@ -19,6 +19,9 @@ use rlp::SHA3_NULL_RLP;
/// A key-value datastore implemented as a database-backed modified Merkle tree. /// A key-value datastore implemented as a database-backed modified Merkle tree.
pub trait Trie { pub trait Trie {
/// Returns an iterator over elements of trie.
fn iter<'a>(&'a self) -> Box<Iterator<Item = (Vec<u8>, &[u8])> + 'a> { unimplemented!() }
/// Return the root of the trie. /// Return the root of the trie.
fn root(&self) -> &H256; fn root(&self) -> &H256;