Secure trie functions & structs.
This commit is contained in:
parent
0125125c27
commit
8bf03bb8ff
@ -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]
|
||||
|
@ -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::*;
|
||||
|
@ -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.
|
||||
|
@ -77,6 +77,41 @@ pub fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> 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<u8>, Vec<u8>)>) -> 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<u8>, Vec<u8>)>) -> H256 {
|
||||
let mut stream = RlpStream::new();
|
||||
hash256rlp(&input, 0, &mut stream);
|
||||
|
Loading…
Reference in New Issue
Block a user