2016-02-05 13:40:41 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2016-02-01 15:22:42 +01:00
|
|
|
//! Trie interface and implementation.
|
|
|
|
|
2016-06-07 20:44:09 +02:00
|
|
|
use std::fmt;
|
2016-06-27 10:59:59 +02:00
|
|
|
use hash::H256;
|
|
|
|
use hashdb::HashDB;
|
2016-06-07 20:44:09 +02:00
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the trietraits module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod trietraits;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the standardmap module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod standardmap;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the journal module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod journal;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the node module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod node;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the triedb module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod triedb;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the triedbmut module.
|
2015-12-19 13:35:26 +01:00
|
|
|
pub mod triedbmut;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the sectriedb module.
|
2016-01-06 13:53:23 +01:00
|
|
|
pub mod sectriedb;
|
2016-02-03 14:51:45 +01:00
|
|
|
/// Export the sectriedbmut module.
|
2016-01-06 13:53:23 +01:00
|
|
|
pub mod sectriedbmut;
|
2015-12-19 13:35:26 +01:00
|
|
|
|
2016-06-27 09:16:34 +02:00
|
|
|
mod fatdb;
|
2016-06-27 10:59:59 +02:00
|
|
|
mod fatdbmut;
|
|
|
|
|
2016-06-07 20:44:09 +02:00
|
|
|
pub use self::trietraits::{Trie, TrieMut};
|
|
|
|
pub use self::standardmap::{Alphabet, StandardMap, ValueMode};
|
|
|
|
pub use self::triedbmut::TrieDBMut;
|
2016-06-27 09:16:34 +02:00
|
|
|
pub use self::triedb::{TrieDB, TrieDBIterator};
|
2016-06-07 20:44:09 +02:00
|
|
|
pub use self::sectriedbmut::SecTrieDBMut;
|
|
|
|
pub use self::sectriedb::SecTrieDB;
|
2016-06-27 09:16:34 +02:00
|
|
|
pub use self::fatdb::{FatDB, FatDBIterator};
|
2016-06-27 10:59:59 +02:00
|
|
|
pub use self::fatdbmut::FatDBMut;
|
2016-06-07 20:44:09 +02:00
|
|
|
|
|
|
|
/// Trie Errors
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum TrieError {
|
|
|
|
/// Attempted to create a trie with a state root not in the DB.
|
|
|
|
InvalidStateRoot,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for TrieError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Trie Error: Invalid state root.")
|
|
|
|
}
|
2016-06-27 09:16:34 +02:00
|
|
|
}
|
2016-06-27 10:59:59 +02:00
|
|
|
|
|
|
|
/// Trie types
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-06-27 10:59:59 +02:00
|
|
|
pub enum TrieSpec {
|
2016-06-27 11:19:27 +02:00
|
|
|
/// Generic trie.
|
|
|
|
Generic,
|
2016-06-27 10:59:59 +02:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
2016-07-04 13:53:55 +02:00
|
|
|
#[cfg_attr(feature="dev", allow(wrong_self_convention))]
|
2016-06-27 10:59:59 +02:00
|
|
|
impl TrieFactory {
|
|
|
|
/// Creates new factory.
|
|
|
|
pub fn new(spec: TrieSpec) -> Self {
|
|
|
|
TrieFactory {
|
|
|
|
spec: spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new immutable instance of Trie.
|
2016-06-27 13:50:08 +02:00
|
|
|
pub fn readonly<'db>(&self, db: &'db HashDB, root: &'db H256) -> Result<Box<Trie + 'db>, TrieError> {
|
2016-06-27 10:59:59 +02:00
|
|
|
match self.spec {
|
2016-06-27 11:19:27 +02:00
|
|
|
TrieSpec::Generic => Ok(Box::new(try!(TrieDB::new(db, root)))),
|
2016-06-27 10:59:59 +02:00
|
|
|
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.
|
2016-06-27 13:50:08 +02:00
|
|
|
pub fn create<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Box<TrieMut + 'db> {
|
2016-06-27 10:59:59 +02:00
|
|
|
match self.spec {
|
2016-06-27 11:19:27 +02:00
|
|
|
TrieSpec::Generic => Box::new(TrieDBMut::new(db, root)),
|
|
|
|
TrieSpec::Secure => Box::new(SecTrieDBMut::new(db, root)),
|
|
|
|
TrieSpec::Fat => Box::new(FatDBMut::new(db, root)),
|
2016-06-27 10:59:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new mutable instance of trie and check for errors.
|
2016-06-27 11:19:27 +02:00
|
|
|
pub fn from_existing<'db>(&self, db: &'db mut HashDB, root: &'db mut H256) -> Result<Box<TrieMut + 'db>, TrieError> {
|
2016-06-27 10:59:59 +02:00
|
|
|
match self.spec {
|
2016-06-27 11:19:27 +02:00
|
|
|
TrieSpec::Generic => Ok(Box::new(try!(TrieDBMut::from_existing(db, root)))),
|
2016-06-27 10:59:59 +02:00
|
|
|
TrieSpec::Secure => Ok(Box::new(try!(SecTrieDBMut::from_existing(db, root)))),
|
|
|
|
TrieSpec::Fat => Ok(Box::new(try!(FatDBMut::from_existing(db, root)))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|