From 8bf03bb8ff334e0a0b2929c13a35b581f86af7b8 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 6 Jan 2016 13:53:23 +0100 Subject: [PATCH] Secure trie functions & structs. --- src/rlp/tests.rs | 2 +- src/trie/mod.rs | 4 ++++ src/trie/triedbmut.rs | 24 ++++++++++++------------ src/triehash.rs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/rlp/tests.rs b/src/rlp/tests.rs index b4a60a3de..39af862ff 100644 --- a/src/rlp/tests.rs +++ b/src/rlp/tests.rs @@ -4,7 +4,7 @@ use self::json_tests::rlp as rlptest; use std::{fmt, cmp}; use std::str::FromStr; use rlp; -use rlp::{UntrustedRlp, RlpStream, Decodable, View, Stream, Encodable}; +use rlp::{UntrustedRlp, RlpStream, View, Stream}; use uint::U256; #[test] diff --git a/src/trie/mod.rs b/src/trie/mod.rs index 4469926d5..1b5919bd4 100644 --- a/src/trie/mod.rs +++ b/src/trie/mod.rs @@ -4,8 +4,12 @@ pub mod journal; pub mod node; pub mod triedb; pub mod triedbmut; +pub mod sectriedb; +pub mod sectriedbmut; pub use self::trietraits::*; pub use self::standardmap::*; pub use self::triedbmut::*; pub use self::triedb::*; +pub use self::sectriedbmut::*; +pub use self::sectriedb::*; diff --git a/src/trie/triedbmut.rs b/src/trie/triedbmut.rs index 004912a4c..4c78e7667 100644 --- a/src/trie/triedbmut.rs +++ b/src/trie/triedbmut.rs @@ -9,18 +9,6 @@ use super::node::*; use super::journal::*; use super::trietraits::*; -pub struct TrieDBMut<'db> { - db: &'db mut HashDB, - root: &'db mut H256, - pub hash_count: usize, -} - -/// Option-like type allowing either a Node object passthrough or Bytes in the case of data alteration. -enum MaybeChanged<'a> { - Same(Node<'a>), - Changed(Bytes), -} - /// A `Trie` implementation using a generic `HashDB` backing database. /// /// Use it as a `Trie` trait object. You can use `db()` to get the backing database object, `keys` @@ -52,6 +40,18 @@ enum MaybeChanged<'a> { /// assert!(t.db_items_remaining().is_empty()); /// } /// ``` +pub struct TrieDBMut<'db> { + db: &'db mut HashDB, + root: &'db mut H256, + pub hash_count: usize, +} + +/// Option-like type allowing either a Node object passthrough or Bytes in the case of data alteration. +enum MaybeChanged<'a> { + Same(Node<'a>), + Changed(Bytes), +} + impl<'db> TrieDBMut<'db> { /// Create a new trie with the backing database `db` and empty `root` /// Initialise to the state entailed by the genesis block. diff --git a/src/triehash.rs b/src/triehash.rs index 67ffcc666..74900971c 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -77,6 +77,41 @@ pub fn trie_root(input: Vec<(Vec, Vec)>) -> H256 { gen_trie_root(gen_input) } +/// Generates a key-hashed (secure) trie root hash for a vector of key-values. +/// +/// ```rust +/// extern crate ethcore_util as util; +/// use std::str::FromStr; +/// use util::triehash::*; +/// use util::hash::*; +/// +/// fn main() { +/// let v = vec![ +/// (From::from("doe"), From::from("reindeer")), +/// (From::from("dog"), From::from("puppy")), +/// (From::from("dogglesworth"), From::from("cat")), +/// ]; +/// +/// let root = "d4cd937e4a4368d7931a9cf51686b7e10abb3dce38a39000fd7902a092b64585"; +/// assert_eq!(sec_trie_root(v), H256::from_str(root).unwrap()); +/// } +/// ``` +pub fn sec_trie_root(input: Vec<(Vec, Vec)>) -> H256 { + let gen_input = input + // first put elements into btree to sort them and to remove duplicates + .into_iter() + .fold(BTreeMap::new(), | mut acc, (k, v) | { + acc.insert(k.sha3().to_vec(), v); + acc + }) + // then move them to a vector + .into_iter() + .map(|(k, v)| (as_nibbles(&k), v) ) + .collect(); + + gen_trie_root(gen_input) +} + fn gen_trie_root(input: Vec<(Vec, Vec)>) -> H256 { let mut stream = RlpStream::new(); hash256rlp(&input, 0, &mut stream);