diff --git a/src/rlp.rs b/src/rlp.rs index 81355fb5d..e8562828a 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -348,7 +348,7 @@ impl<'a> UntrustedRlp<'a> { /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// } /// ``` - pub fn data(&'a self) -> &'a [u8] { + pub fn data(&self) -> &[u8] { self.bytes } @@ -1275,6 +1275,14 @@ mod tests { run_encode_tests(tests); } + #[test] + fn encode_bytes() { + let vec = vec![0u8]; + let slice: &[u8] = &vec; + let res = rlp::encode(&slice); + assert_eq!(res, vec![0u8]); + } + #[test] fn rlp_stream() { let mut stream = RlpStream::new_list(2); diff --git a/src/trie.rs b/src/trie.rs index c204ce590..4aebc688f 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -89,7 +89,7 @@ impl TrieDB { }, Prototype::List(2) => { let their_key_rlp = o.at(0); - let (them, _) = NibbleSlice::from_encoded(o.data()); + let (them, _) = NibbleSlice::from_encoded(their_key_rlp.data()); match partial_key.common_prefix(&them) { 0 => { // transmute to branch here @@ -97,7 +97,7 @@ impl TrieDB { cp if cp == them.len() => { // fast-forward }, - cp => { + _ => { // cleve into two + branch in the middle }, } @@ -156,5 +156,5 @@ fn playpen() { assert!(t.is_empty()); t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]); - assert_eq!(*t.root(), hash256(&[NibblePair::new_raw(vec![0x01u8, 0x23], vec![0x01u8, 0x23])])); -} \ No newline at end of file + assert_eq!(*t.root(), trie_root(vec![ (vec![1u8, 0x23], vec![1u8, 0x23]) ])); +} diff --git a/src/triehash.rs b/src/triehash.rs index aaeef5f65..bc8dedf76 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -1,4 +1,6 @@ -//! Generete trie root +//! Generete trie root. +//! +//! This module should be used to generate trie root hash. use std::collections::BTreeMap; use std::cmp; @@ -8,6 +10,73 @@ use rlp; use rlp::RlpStream; use vector::SharedPrefix; +// todo: verify if example for ordered_trie_root is valid +/// Generates a trie root hash for a vector of 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")]; +/// let root = "e766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3"; +/// assert_eq!(ordered_trie_root(v), H256::from_str(root).unwrap()); +/// } +/// ``` +pub fn ordered_trie_root(input: Vec>) -> H256 { + let gen_input = input + // first put elements into btree to sort them by nibbles + // optimize it later + .into_iter() + .fold(BTreeMap::new(), | mut acc, vec | { + let len = acc.len(); + acc.insert(as_nibbles(&rlp::encode(&len)), vec); + acc + }) + // then move them to a vector + .into_iter() + .map(|p| p ) + .collect(); + + gen_trie_root(gen_input) +} + +/// Generates a 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 = "8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3"; +/// assert_eq!(trie_root(v), H256::from_str(root).unwrap()); +/// } +/// ``` +pub fn trie_root(input: Vec<(Vec, Vec)>) -> H256 { + let gen_input = input + .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); + stream.out().sha3() +} + /// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. /// /// The "termination marker" and "leaf-node" specifier are completely equivalent. @@ -27,45 +96,7 @@ use vector::SharedPrefix; /// [1,2,3,4,5,T] 0x312345 // 5 > 3 /// [1,2,3,4,T] 0x201234 // 4 > 3 /// ``` -/// -/// ```rust -/// extern crate ethcore_util as util; -/// use util::triehash::*; -/// -/// fn main() { -/// let v = vec![0, 0, 1, 2, 3, 4, 5]; -/// let e = vec![0x10, 0x01, 0x23, 0x45]; -/// let h = hex_prefix_encode(&v, false); -/// assert_eq!(h, e); -/// -/// let v = vec![0, 1, 2, 3, 4, 5]; -/// let e = vec![0x00, 0x01, 0x23, 0x45]; -/// let h = hex_prefix_encode(&v, false); -/// assert_eq!(h, e); -/// -/// let v = vec![0, 1, 2, 3, 4, 5]; -/// let e = vec![0x20, 0x01, 0x23, 0x45]; -/// let h = hex_prefix_encode(&v, true); -/// assert_eq!(h, e); -/// -/// let v = vec![1, 2, 3, 4, 5]; -/// let e = vec![0x31, 0x23, 0x45]; -/// let h = hex_prefix_encode(&v, true); -/// assert_eq!(h, e); -/// -/// let v = vec![1, 2, 3, 4]; -/// let e = vec![0x00, 0x12, 0x34]; -/// let h = hex_prefix_encode(&v, false); -/// assert_eq!(h, e); -/// -/// let v = vec![4, 1]; -/// let e = vec![0x20, 0x41]; -/// let h = hex_prefix_encode(&v, true); -/// assert_eq!(h, e); -/// } -/// ``` -/// -pub fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec { +fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec { let inlen = nibbles.len(); let oddness_factor = inlen % 2; // next even number divided by two @@ -94,23 +125,7 @@ pub fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec { } /// Converts slice of bytes to nibbles. -/// -/// ```rust -/// extern crate ethcore_util as util; -/// use util::triehash::*; -/// -/// fn main () { -/// let v = vec![0x31, 0x23, 0x45]; -/// let e = vec![3, 1, 2, 3, 4, 5]; -/// assert_eq!(as_nibbles(&v), e); -/// -/// // A => 65 => 0x41 => [4, 1] -/// let v: Vec = From::from("A"); -/// let e = vec![4, 1]; -/// assert_eq!(as_nibbles(&v), e); -/// } -/// ``` -pub fn as_nibbles(bytes: &[u8]) -> Vec { +fn as_nibbles(bytes: &[u8]) -> Vec { let mut res = vec![]; res.reserve(bytes.len() * 2); for i in 0..bytes.len() { @@ -120,98 +135,44 @@ pub fn as_nibbles(bytes: &[u8]) -> Vec { res } -#[derive(Debug)] -pub struct NibblePair { - nibble: Vec, - data: Vec -} - -impl NibblePair { - pub fn new(nibble: Vec, data: Vec) -> NibblePair { - NibblePair { - nibble: nibble, - data: data - } - } - - pub fn new_raw(to_nibble: Vec, data: Vec) -> NibblePair { - NibblePair::new(as_nibbles(&to_nibble), data) - } -} - -fn nibble_shared_prefix_len(vec: &[NibblePair]) -> usize { - if vec.len() == 0 { - return 0; - } - - vec.iter() - // skip first element - .skip(1) - // get minimum number of shared nibbles between first and each successive - .fold(vec[0].nibble.len(), | acc, pair | { - cmp::min(vec[0].nibble.shared_prefix_len(&pair.nibble), acc) - }) -} - -pub fn ordered_trie_root(data: Vec>) -> H256 { - let vec: Vec = data - // first put elements into btree to sort them by nibbles - // optimize it later - .into_iter() - .fold(BTreeMap::new(), | mut acc, vec | { - let len = acc.len(); - acc.insert(as_nibbles(&rlp::encode(&len)), vec); - acc - }) - // then move them to a vector - .into_iter() - .map(|(k, v)| NibblePair::new(k, v) ) - .collect(); - - hash256(&vec) -} - -pub fn hash256(vec: &[NibblePair]) -> H256 { - let out = match vec.len() { - 0 => rlp::encode(&""), - _ => { - let mut stream = RlpStream::new(); - hash256rlp(&vec, 0, &mut stream); - stream.out() - } - }; - - out.sha3() -} - -fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { - let vlen = vec.len(); +fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStream) { + let inlen = input.len(); // in case of empty slice, just append null - if vlen == 0 { + if inlen == 0 { stream.append(&""); return; } + // take slices + let key: &Vec = &input[0].0; + let value: &[u8] = &input[0].1; + // if the slice contains just one item, append the suffix of the key // and then append value - if vlen == 1 { + if inlen == 1 { stream.append_list(2); - stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true)); - stream.append(&vec[0].data); + stream.append(&hex_prefix_encode(&key[pre_len..], true)); + stream.append(&value); return; } // get length of the longest shared prefix in slice keys - let shared_prefix = nibble_shared_prefix_len(vec); + let shared_prefix = input.iter() + // skip first element + .skip(1) + // get minimum number of shared nibbles between first and each successive + .fold(key.len(), | acc, &(ref k, _) | { + cmp::min(key.shared_prefix_len(&k), acc) + }); // if shared prefix is higher than current prefix append its // new part of the key to the stream // then recursively append suffixes of all items who had this key if shared_prefix > pre_len { stream.append_list(2); - stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false)); - hash256aux(vec, shared_prefix, stream); + stream.append(&hex_prefix_encode(&key[pre_len..shared_prefix], false)); + hash256aux(input, shared_prefix, stream); return; } @@ -220,7 +181,7 @@ fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { stream.append_list(17); // if first key len is equal to prefix_len, move to next element - let mut begin = match pre_len == vec[0].nibble.len() { + let mut begin = match pre_len == key.len() { true => 1, false => 0 }; @@ -228,29 +189,29 @@ fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { // iterate over all possible nibbles for i in 0..16 { // cout how many successive elements have same next nibble - let len = vec[begin..].iter() - .map(| pair | pair.nibble[pre_len] ) + let len = input[begin..].iter() + .map(| pair | pair.0[pre_len] ) .take_while(|&q| q == i).count(); // if at least 1 successive element has the same nibble // append their suffixes match len { 0 => { stream.append(&""); }, - _ => hash256aux(&vec[begin..(begin + len)], pre_len + 1, stream) + _ => hash256aux(&input[begin..(begin + len)], pre_len + 1, stream) } begin += len; } // if fist key len is equal prefix, append it's value - match pre_len == vec[0].nibble.len() { - true => { stream.append(&vec[0].data); }, + match pre_len == key.len() { + true => { stream.append(&value); }, false => { stream.append(&""); } }; } -fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { +fn hash256aux(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStream) { let mut s = RlpStream::new(); - hash256rlp(vec, pre_len, &mut s); + hash256rlp(input, pre_len, &mut s); let out = s.out(); match out.len() { 0...31 => stream.append_raw(&out, 1), @@ -260,35 +221,48 @@ fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { #[test] -fn test_shared_nibble_len() { - let len = nibble_shared_prefix_len(&vec![ - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - ]); +fn test_nibbles() { + let v = vec![0x31, 0x23, 0x45]; + let e = vec![3, 1, 2, 3, 4, 5]; + assert_eq!(as_nibbles(&v), e); - assert_eq!(len , 7); + // A => 65 => 0x41 => [4, 1] + let v: Vec = From::from("A"); + let e = vec![4, 1]; + assert_eq!(as_nibbles(&v), e); } #[test] -fn test_shared_nibble_len2() { - let len = nibble_shared_prefix_len(&vec![ - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - NibblePair::new(vec![4, 1, 2, 3, 4, 5, 6], vec![]) - ]); +fn test_hex_prefix_encode() { + let v = vec![0, 0, 1, 2, 3, 4, 5]; + let e = vec![0x10, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); - assert_eq!(len , 0); -} + let v = vec![0, 1, 2, 3, 4, 5]; + let e = vec![0x00, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); -#[test] -fn test_shared_nibble_len3() { - let len = nibble_shared_prefix_len(&vec![ - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]), - NibblePair::new(vec![0, 1, 2, 4, 4, 5, 6], vec![]) - ]); + let v = vec![0, 1, 2, 3, 4, 5]; + let e = vec![0x20, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); - assert_eq!(len , 3); + let v = vec![1, 2, 3, 4, 5]; + let e = vec![0x31, 0x23, 0x45]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); + + let v = vec![1, 2, 3, 4]; + let e = vec![0x00, 0x12, 0x34]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); + + let v = vec![4, 1]; + let e = vec![0x20, 0x41]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); } #[cfg(test)] @@ -298,106 +272,83 @@ mod tests { use hash::*; use triehash::*; - - #[test] fn empty_trie_root() { - assert_eq!(hash256(&vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); + assert_eq!(trie_root(vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); } #[test] fn single_trie_item() { - - let v = vec![ - NibblePair::new_raw(From::from("A"), - From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - ]; - - assert_eq!(hash256(&v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); + let v = vec![(From::from("A"), From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))]; + assert_eq!(trie_root(v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); } #[test] fn foo_trie_item() { let v = vec![ - NibblePair::new_raw(From::from("foo"), - From::from("bar")), - NibblePair::new_raw(From::from("food"), - From::from("bass")) + (From::from("foo"), From::from("bar")), + (From::from("food"), From::from("bass")) ]; - assert_eq!(hash256(&v), H256::from_str("17beaa1648bafa633cda809c90c04af50fc8aed3cb40d16efbddee6fdf63c4c3").unwrap()); + assert_eq!(trie_root(v), H256::from_str("17beaa1648bafa633cda809c90c04af50fc8aed3cb40d16efbddee6fdf63c4c3").unwrap()); } #[test] fn dogs_trie_item() { let v = vec![ - NibblePair::new_raw(From::from("doe"), - From::from("reindeer")), - - NibblePair::new_raw(From::from("dog"), - From::from("puppy")), - - NibblePair::new_raw(From::from("dogglesworth"), - From::from("cat")), + (From::from("doe"), From::from("reindeer")), + (From::from("dog"), From::from("puppy")), + (From::from("dogglesworth"), From::from("cat")), ]; - assert_eq!(hash256(&v), H256::from_str("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3").unwrap()); + assert_eq!(trie_root(v), H256::from_str("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3").unwrap()); } #[test] fn puppy_trie_items() { let v = vec![ - NibblePair::new_raw(From::from("do"), - From::from("verb")), - - NibblePair::new_raw(From::from("dog"), - From::from("puppy")), - - NibblePair::new_raw(From::from("doge"), - From::from("coin")), - - NibblePair::new_raw(From::from("horse"), - From::from("stallion")), - + (From::from("do"), From::from("verb")), + (From::from("dog"), From::from("puppy")), + (From::from("doge"), From::from("coin")), + (From::from("horse"), From::from("stallion")), ]; - assert_eq!(hash256(&v), H256::from_str("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84").unwrap()); + assert_eq!(trie_root(v), H256::from_str("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84").unwrap()); } #[test] fn test_trie_root() { let v = vec![ - NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), - "22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), + + ("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), + "22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), - NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), - "67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + ("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), + "67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - NibblePair::new_raw("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), - "6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), + ("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), + "6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), - NibblePair::new_raw("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), - "4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + ("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), + "4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - NibblePair::new_raw("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), - "4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), + ("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), + "4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), - NibblePair::new_raw("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), - "7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), + ("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), + "7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), - NibblePair::new_raw("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), - "ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()), + ("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), + "ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()), + + ("6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap(), + "697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap()) - NibblePair::new_raw("6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap(), - "697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap()) ]; - let root = H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap(); - - let res = hash256(&v); - assert_eq!(res, root); + assert_eq!(trie_root(v), H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap()); } }