From f7ec667852fc4641ac76d01c70a4bcffc9957182 Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 29 Nov 2015 18:44:29 +0100 Subject: [PATCH 1/3] in pro... --- src/bytes.rs | 56 +++---- src/rlp.rs | 90 +++++----- src/triehash.rs | 435 +++++++++++++++++++++++++++++++----------------- src/vector.rs | 37 +++- 4 files changed, 391 insertions(+), 227 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index c08c4ee2a..03815524e 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -104,27 +104,27 @@ impl ToBytes for String { fn to_bytes_len(&self) -> usize { self.len() } } -impl ToBytes for u8 { - fn to_bytes(&self) -> Vec { - match *self { - 0 => vec![], - _ => vec![*self] - } - } +//impl ToBytes for u8 { + //fn to_bytes(&self) -> Vec { + //match *self { + //0 => vec![], + //_ => vec![*self] + //} + //} - fn to_bytes_len(&self) -> usize { - match *self { - 0 => 0, - _ => 1 - } - } - fn first_byte(&self) -> Option { - match *self { - 0 => None, - _ => Some(*self) - } - } -} + //fn to_bytes_len(&self) -> usize { + //match *self { + //0 => 0, + //_ => 1 + //} + //} + //fn first_byte(&self) -> Option { + //match *self { + //0 => None, + //_ => Some(*self) + //} + //} +//} impl ToBytes for u64 { fn to_bytes(&self) -> Vec { @@ -223,14 +223,14 @@ impl FromBytes for String { } } -impl FromBytes for u8 { - fn from_bytes(bytes: &[u8]) -> FromBytesResult { - match bytes.len() { - 0 => Ok(0), - _ => Ok(bytes[0]) - } - } -} +//impl FromBytes for u8 { + //fn from_bytes(bytes: &[u8]) -> FromBytesResult { + //match bytes.len() { + //0 => Ok(0), + //_ => Ok(bytes[0]) + //} + //} +//} impl FromBytes for u64 { fn from_bytes(bytes: &[u8]) -> FromBytesResult { diff --git a/src/rlp.rs b/src/rlp.rs index 5b08aca2b..de31dcb4a 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -478,7 +478,7 @@ impl RlpStream { } /// Appends raw (pre-serialised) RLP data. Use with caution. Chainable. - pub fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a RlpStream { + pub fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream { // push raw items self.encoder.bytes.extend(bytes); @@ -745,19 +745,19 @@ mod tests { } } - #[test] - fn encode_u8() { - let tests = vec![ - ETestPair(0u8, vec![0x80u8]), - ETestPair(15, vec![15]), - ETestPair(55, vec![55]), - ETestPair(56, vec![56]), - ETestPair(0x7f, vec![0x7f]), - ETestPair(0x80, vec![0x81, 0x80]), - ETestPair(0xff, vec![0x81, 0xff]), - ]; - run_encode_tests(tests); - } + //#[test] + //fn encode_u8() { + //let tests = vec![ + //ETestPair(0u8, vec![0x80u8]), + //ETestPair(15, vec![15]), + //ETestPair(55, vec![55]), + //ETestPair(56, vec![56]), + //ETestPair(0x7f, vec![0x7f]), + //ETestPair(0x80, vec![0x81, 0x80]), + //ETestPair(0xff, vec![0x81, 0xff]), + //]; + //run_encode_tests(tests); + //} #[test] fn encode_u16() { @@ -835,15 +835,15 @@ mod tests { run_encode_tests(tests); } - #[test] - fn encode_vector_u8() { - let tests = vec![ - ETestPair(vec![], vec![0xc0]), - ETestPair(vec![15u8], vec![0xc1, 0x0f]), - ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), - ]; - run_encode_tests(tests); - } + //#[test] + //fn encode_vector_u8() { + //let tests = vec![ + //ETestPair(vec![], vec![0xc0]), + //ETestPair(vec![15u8], vec![0xc1, 0x0f]), + //ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), + //]; + //run_encode_tests(tests); + //} #[test] fn encode_vector_u64() { @@ -899,19 +899,19 @@ mod tests { } } - #[test] - fn decode_untrusted_u8() { - let tests = vec![ - DTestPair(0u8, vec![0u8]), - DTestPair(15, vec![15]), - DTestPair(55, vec![55]), - DTestPair(56, vec![56]), - DTestPair(0x7f, vec![0x7f]), - DTestPair(0x80, vec![0x81, 0x80]), - DTestPair(0xff, vec![0x81, 0xff]), - ]; - run_decode_untrusted_tests(tests); - } + //#[test] + //fn decode_untrusted_u8() { + //let tests = vec![ + //DTestPair(0u8, vec![0u8]), + //DTestPair(15, vec![15]), + //DTestPair(55, vec![55]), + //DTestPair(56, vec![56]), + //DTestPair(0x7f, vec![0x7f]), + //DTestPair(0x80, vec![0x81, 0x80]), + //DTestPair(0xff, vec![0x81, 0xff]), + //]; + //run_decode_untrusted_tests(tests); + //} #[test] fn decode_untrusted_u16() { @@ -991,15 +991,15 @@ mod tests { run_decode_untrusted_tests(tests); } - #[test] - fn decode_untrusted_vector_u8() { - let tests = vec![ - DTestPair(vec![] as Vec, vec![0xc0]), - DTestPair(vec![15u8], vec![0xc1, 0x0f]), - DTestPair(vec![1u8, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), - ]; - run_decode_untrusted_tests(tests); - } + //#[test] + //fn decode_untrusted_vector_u8() { + //let tests = vec![ + //DTestPair(vec![] as Vec, vec![0xc0]), + //DTestPair(vec![15u8], vec![0xc1, 0x0f]), + //DTestPair(vec![1u8, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), + //]; + //run_decode_untrusted_tests(tests); + //} #[test] fn decode_untrusted_vector_u64() { diff --git a/src/triehash.rs b/src/triehash.rs index 7059cb5f9..6f9490cc1 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -1,171 +1,300 @@ //! Generete trie root -use std::collections::BTreeMap; -use std::cmp; -use hash::*; -use sha3::*; -use rlp; -use rlp::RlpStream; +//use std::collections::BTreeMap; +//use std::cmp; +//use hash::*; +//use sha3::*; +//use rlp; +//use rlp::RlpStream; +//use vector::SharedPrefix; -/// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. -/// -/// The "termination marker" and "leaf-node" specifier are completely equivalent. -/// -/// Input values are in range `[0, 0xf]`. -/// -/// ```markdown -/// [0,0,1,2,3,4,5] 0x10012345 // 7 > 4 -/// [0,1,2,3,4,5] 0x00012345 // 6 > 4 -/// [1,2,3,4,5] 0x112345 // 5 > 3 -/// [0,0,1,2,3,4] 0x00001234 // 6 > 3 -/// [0,1,2,3,4] 0x101234 // 5 > 3 -/// [1,2,3,4] 0x001234 // 4 > 3 -/// [0,0,1,2,3,4,5,T] 0x30012345 // 7 > 4 -/// [0,0,1,2,3,4,T] 0x20001234 // 6 > 4 -/// [0,1,2,3,4,5,T] 0x20012345 // 6 > 4 -/// [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); -/// } -/// ``` -/// -pub fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec { - let inlen = nibbles.len(); - let oddness_factor = inlen % 2; - // next even number divided by two - let reslen = (inlen + 2) >> 1; - let mut res = vec![]; - res.reserve(reslen); +///// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. +///// +///// The "termination marker" and "leaf-node" specifier are completely equivalent. +///// +///// Input values are in range `[0, 0xf]`. +///// +///// ```markdown +///// [0,0,1,2,3,4,5] 0x10012345 // 7 > 4 +///// [0,1,2,3,4,5] 0x00012345 // 6 > 4 +///// [1,2,3,4,5] 0x112345 // 5 > 3 +///// [0,0,1,2,3,4] 0x00001234 // 6 > 3 +///// [0,1,2,3,4] 0x101234 // 5 > 3 +///// [1,2,3,4] 0x001234 // 4 > 3 +///// [0,0,1,2,3,4,5,T] 0x30012345 // 7 > 4 +///// [0,0,1,2,3,4,T] 0x20001234 // 6 > 4 +///// [0,1,2,3,4,5,T] 0x20012345 // 6 > 4 +///// [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 { + //let inlen = nibbles.len(); + //let oddness_factor = inlen % 2; + //// next even number divided by two + //let reslen = (inlen + 2) >> 1; + //let mut res = vec![]; + //res.reserve(reslen); - let first_byte = { - let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; - if oddness_factor == 1 { - bits += nibbles[0]; - } - bits - }; + //let first_byte = { + //let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; + //if oddness_factor == 1 { + //bits += nibbles[0]; + //} + //bits + //}; - res.push(first_byte); + //res.push(first_byte); - let mut offset = oddness_factor; - while offset < inlen { - let byte = (nibbles[offset] << 4) + nibbles[offset + 1]; - res.push(byte); - offset += 2; - } + //let mut offset = oddness_factor; + //while offset < inlen { + //let byte = (nibbles[offset] << 4) + nibbles[offset + 1]; + //res.push(byte); + //offset += 2; + //} - res -} + //res +//} -/// 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); -/// } -/// ``` -pub fn as_nibbles(bytes: &[u8]) -> Vec { - let mut res = vec![]; - res.reserve(bytes.len() * 2); - for i in 0..bytes.len() { - res.push(bytes[i] >> 4); - res.push((bytes[i] << 4) >> 4); - } - res -} +///// 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 { + //let mut res = vec![]; + //res.reserve(bytes.len() * 2); + //for i in 0..bytes.len() { + //res.push(bytes[i] >> 4); + //res.push((bytes[i] << 4) >> 4); + //} + //res +//} -struct NibblePair { - nibble: Vec, - data: Vec -} +//#[derive(Debug)] +//pub struct NibblePair { + //nibble: Vec, + //data: Vec +//} -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 { nibble: k, data: v } ) - .collect(); +//impl NibblePair { + //pub fn new(nibble: Vec, data: Vec) -> NibblePair { + //NibblePair { + //nibble: nibble, + //data: data + //} + //} - let out = match vec.len() { - 0 => rlp::encode(&""), - _ => { - let mut stream = RlpStream::new(); - hash256rlp(&vec, 0, &mut stream); - stream.out().unwrap() - } - }; + //pub fn new_raw(to_nibble: Vec, data: Vec) -> NibblePair { + //NibblePair::new(as_nibbles(&to_nibble), data) + //} +//} + +//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().unwrap() + //} + //}; - out.sha3() -} + //println!("out: {:?}", out); + //out.sha3() +//} -fn shared_prefix_length(v1: &[T], v2: &[T]) -> usize where T: Eq { - let len = cmp::min(v1.len(), v2.len()); - (0..len).take_while(|&i| v1[i] == v2[i]).count() -} +//fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { + //match vec.len() { + //0 => { + //stream.append(&""); + //}, + //1 => { + //stream.append_list(2); + //stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true)); + //stream.append(&vec[0].data); + //}, + //_ => { + //let shared_prefix = vec.iter() + //// skip first element + //.skip(1) + //// get minimum number of shared nibbles between first and each successive + //.fold(usize::max_value(), | acc, pair | { + //cmp::min(vec[0].nibble.shared_prefix_len(&pair.nibble), acc) + //}); -fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { - match vec.len() { - 0 => stream.append(&""), - 1 => stream.append_list(2).append(&hex_prefix_encode(&vec[0].nibble, true)).append(&vec[0].data), - _ => { - let shared_prefix = vec.iter() - // skip first element - .skip(1) - // get minimum number of shared nibbles between first string and each successive - .fold(usize::max_value(), | acc, pair | cmp::min(shared_prefix_length(&vec[0].nibble, &pair.nibble), acc) ); //match shared_prefix > pre_len { + //true => { + //stream.append_list(2); + //stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false)); + //hash256aux(vec, shared_prefix, stream); + //}, + //false => { + //stream.append_list(17); - //true => hex_prefix_encode(&vec[0].nibble + //// every nibble is longer then previous one + //let iter = vec.iter() + //// move to first element with len different then pre_len + //.take_while(| pair | { pair.nibble.len() == pre_len }); + + //let mut begin = iter.count(); + + //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] ) + //.take_while(|&q| q == i).count(); + + //match len { + //0 => { stream.append(&""); }, + //_ => hash256aux(&vec[begin..begin + len], pre_len + 1, stream) + //} + //begin += len; + //} + + //match pre_len == vec[0].nibble.len() { + //true => stream.append(&vec[0].data), + //false => stream.append(&"") + //}; + //} //} - panic!(); - - } - }; -} + //} + //}; +//} -#[cfg(test)] -mod tests { -} +//fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { + //let mut s = RlpStream::new(); + //hash256rlp(vec, pre_len, &mut s); + //let out = s.out().unwrap(); + //match out.len() { + //0...31 => stream.append_raw(&out, 1), + //_ => stream.append(&out.sha3()) + //}; +//} + +//#[cfg(test)] +//mod tests { + //use std::str::FromStr; + //use rustc_serialize::hex::FromHex; + //use hash::*; + //use triehash::*; + + //#[test] + //fn empty_trie_root() { + //assert_eq!(hash256(&vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); + //} + + //#[test] + //fn single_trie_item() { + + //let v = vec![ + //NibblePair::new_raw(From::from("A"), + //From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) + //]; + //println!("{:?}", v); + //assert_eq!(hash256(&v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); + //} + + //#[test] + //fn test_trie_root() { + //let v = vec![ + //NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), + //"22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), + + //NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), + //"67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + + //NibblePair::new_raw("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), + //"6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), + + //NibblePair::new_raw("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), + //"4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + + //NibblePair::new_raw("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), + //"4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), + + //NibblePair::new_raw("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), + //"7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), + + //NibblePair::new_raw("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), + //"ec4f34c97e43fbb2816cfd95e388353c7181dab1".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); + + //println!("{:?}", res); + ////assert!(false); + //} +//} diff --git a/src/vector.rs b/src/vector.rs index 7032335cc..41c932c92 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -1,3 +1,5 @@ +//! vector util functions + use std::ptr; pub trait InsertSlice { @@ -32,5 +34,38 @@ impl InsertSlice for Vec { } } -pub trait SharedPreifx { +/// Returns len of prefix shared with elem +/// +/// ```rust +/// extern crate ethcore_util as util; +/// use util::vector::SharedPrefix; +/// +/// fn main () { +/// let a = vec![1,2,3,3,5]; +/// let b = vec![1,2,3]; +/// assert_eq!(a.shared_prefix_len(&b), 3); +/// } +/// ``` +pub trait SharedPrefix { + fn shared_prefix_len(&self, elem: &[T]) -> usize; +} + +impl SharedPrefix for Vec where T: Eq { + fn shared_prefix_len(&self, elem: &[T]) -> usize { + use std::cmp; + let len = cmp::min(self.len(), elem.len()); + (0..len).take_while(|&i| self[i] == elem[i]).count() + } +} + +#[cfg(test)] +mod test { + use vector::SharedPrefix; + + #[test] + fn test_shared_prefix() { + let a = vec![1,2,3,3,5]; + let b = vec![1,2,3]; + assert_eq!(a.shared_prefix_len(&b), 3); + } } From 03c1944d9c422833a2ee7e59ac0879f810b2964c Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 29 Nov 2015 19:35:22 +0100 Subject: [PATCH 2/3] fixed encoding of byte vectors --- src/bytes.rs | 34 +--------- src/rlp.rs | 179 ++++++++++++++++++++++++--------------------------- 2 files changed, 86 insertions(+), 127 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 03815524e..e17ccca9a 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -23,7 +23,7 @@ //! use util::bytes::FromBytes; //! //! let a = String::from_bytes(&[b'd', b'o', b'g']); -//! let b = u8::from_bytes(&[0xfa]); +//! let b = u16::from_bytes(&[0xfa]); //! let c = u64::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); //! } //! @@ -104,28 +104,6 @@ impl ToBytes for String { fn to_bytes_len(&self) -> usize { self.len() } } -//impl ToBytes for u8 { - //fn to_bytes(&self) -> Vec { - //match *self { - //0 => vec![], - //_ => vec![*self] - //} - //} - - //fn to_bytes_len(&self) -> usize { - //match *self { - //0 => 0, - //_ => 1 - //} - //} - //fn first_byte(&self) -> Option { - //match *self { - //0 => None, - //_ => Some(*self) - //} - //} -//} - impl ToBytes for u64 { fn to_bytes(&self) -> Vec { let mut res= vec![]; @@ -223,15 +201,6 @@ impl FromBytes for String { } } -//impl FromBytes for u8 { - //fn from_bytes(bytes: &[u8]) -> FromBytesResult { - //match bytes.len() { - //0 => Ok(0), - //_ => Ok(bytes[0]) - //} - //} -//} - impl FromBytes for u64 { fn from_bytes(bytes: &[u8]) -> FromBytesResult { match bytes.len() { @@ -293,3 +262,4 @@ impl FromBytes for T where T: FixedHash { } } } + diff --git a/src/rlp.rs b/src/rlp.rs index de31dcb4a..feae875e3 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -61,11 +61,11 @@ //! // [ [], [[]], [ [], [[]] ] ] //! let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]; //! let rlp = Rlp::new(&data); -//! let _v0: Vec = Decodable::decode(&rlp.at(0)); -//! let _v1: Vec> = Decodable::decode(&rlp.at(1)); +//! let _v0: Vec = Decodable::decode(&rlp.at(0)); +//! let _v1: Vec> = Decodable::decode(&rlp.at(1)); //! let nested_rlp = rlp.at(2); -//! let _v2a: Vec = Decodable::decode(&nested_rlp.at(0)); -//! let _v2b: Vec> = Decodable::decode(&nested_rlp.at(1)); +//! let _v2a: Vec = Decodable::decode(&nested_rlp.at(0)); +//! let _v2b: Vec> = Decodable::decode(&nested_rlp.at(1)); //! } //! //! fn main() { @@ -343,9 +343,7 @@ pub fn decode(bytes: &[u8]) -> T where T: Decodable { } /// shortcut function to decode UntrustedRlp `&[u8]` into an object -pub fn decode_untrusted(bytes: &[u8]) -> Result - where T: Decodable -{ +pub fn decode_untrusted(bytes: &[u8]) -> Result where T: Decodable { let rlp = UntrustedRlp::new(bytes); T::decode_untrusted(&rlp) } @@ -357,18 +355,18 @@ pub trait Decodable: Sized { } } -impl Decodable for T where T: FromBytes -{ +impl Decodable for T where T: FromBytes { fn decode_untrusted(rlp: &UntrustedRlp) -> Result { match rlp.is_value() { - true => BasicDecoder::read_value(rlp.bytes), + true => BasicDecoder::read_value(rlp.bytes, | bytes | { + Ok(try!(T::from_bytes(bytes))) + }), false => Err(DecoderError::UntrustedRlpExpectedToBeValue), } } } -impl Decodable for Vec where T: Decodable -{ +impl Decodable for Vec where T: Decodable { fn decode_untrusted(rlp: &UntrustedRlp) -> Result { match rlp.is_list() { true => rlp.iter().map(|rlp| T::decode_untrusted(&rlp)).collect(), @@ -377,29 +375,40 @@ impl Decodable for Vec where T: Decodable } } +impl Decodable for Vec { + fn decode_untrusted(rlp: &UntrustedRlp) -> Result { + match rlp.is_value() { + true => BasicDecoder::read_value(rlp.bytes, | bytes | { + let mut res = vec![]; + res.extend(bytes); + Ok(res) + }), + false => Err(DecoderError::UntrustedRlpExpectedToBeValue), + } + } +} + pub trait Decoder { - fn read_value(bytes: &[u8]) -> Result where T: FromBytes; + fn read_value(bytes: &[u8], f: F) -> Result where F: FnOnce(&[u8]) -> Result; } struct BasicDecoder; impl Decoder for BasicDecoder { - fn read_value(bytes: &[u8]) -> Result - where T: FromBytes - { + fn read_value(bytes: &[u8], f: F) -> Result where F: FnOnce(&[u8]) -> Result { match bytes.first().map(|&x| x) { // rlp is too short None => Err(DecoderError::UntrustedRlpIsTooShort), // single byt value - Some(l @ 0...0x7f) => Ok(try!(T::from_bytes(&[l]))), + Some(l @ 0...0x7f) => Ok(try!(f(&[l]))), // 0-55 bytes - Some(l @ 0x80...0xb7) => Ok(try!(T::from_bytes(&bytes[1..(1 + l as usize - 0x80)]))), + Some(l @ 0x80...0xb7) => Ok(try!(f(&bytes[1..(1 + l as usize - 0x80)]))), // longer than 55 bytes Some(l @ 0xb8...0xbf) => { let len_of_len = l as usize - 0xb7; let begin_of_value = 1 as usize + len_of_len; let len = try!(usize::from_bytes(&bytes[1..begin_of_value])); - Ok(try!(T::from_bytes(&bytes[begin_of_value..begin_of_value + len]))) + Ok(try!(f(&bytes[begin_of_value..begin_of_value + len]))) } _ => Err(DecoderError::BadUntrustedRlp), } @@ -525,8 +534,7 @@ impl RlpStream { } /// shortcut function to encode a `T: Encodable` into a UntrustedRlp `Vec` -pub fn encode(object: &E) -> Vec - where E: Encodable +pub fn encode(object: &E) -> Vec where E: Encodable { let mut encoder = BasicEncoder::new(); object.encode(&mut encoder); @@ -559,20 +567,14 @@ pub trait Encoder { fn emit_list(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> (); } -impl Encodable for T where T: ToBytes -{ - fn encode(&self, encoder: &mut E) -> () - where E: Encoder - { +impl Encodable for T where T: ToBytes { + fn encode(&self, encoder: &mut E) -> () where E: Encoder { encoder.emit_value(&self.to_bytes()) } } -impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a -{ - fn encode(&self, encoder: &mut E) -> () - where E: Encoder - { +impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a { + fn encode(&self, encoder: &mut E) -> () where E: Encoder { encoder.emit_list(|e| { // insert all list elements for el in self.iter() { @@ -582,16 +584,29 @@ impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a } } -impl Encodable for Vec where T: Encodable -{ - fn encode(&self, encoder: &mut E) -> () - where E: Encoder - { +impl Encodable for Vec where T: Encodable { + fn encode(&self, encoder: &mut E) -> () where E: Encoder { let r: &[T] = self.as_ref(); r.encode(encoder) } } +/// lets treat bytes differently than other lists +/// they are a single value +impl<'a> Encodable for &'a [u8] { + fn encode(&self, encoder: &mut E) -> () where E: Encoder { + encoder.emit_value(self) + } +} + +/// lets treat bytes differently than other lists +/// they are a single value +impl Encodable for Vec { + fn encode(&self, encoder: &mut E) -> () where E: Encoder { + encoder.emit_value(self) + } +} + struct BasicEncoder { bytes: Vec, } @@ -744,21 +759,7 @@ mod tests { assert_eq!(res, &t.1[..]); } } - - //#[test] - //fn encode_u8() { - //let tests = vec![ - //ETestPair(0u8, vec![0x80u8]), - //ETestPair(15, vec![15]), - //ETestPair(55, vec![55]), - //ETestPair(56, vec![56]), - //ETestPair(0x7f, vec![0x7f]), - //ETestPair(0x80, vec![0x81, 0x80]), - //ETestPair(0xff, vec![0x81, 0xff]), - //]; - //run_encode_tests(tests); - //} - + #[test] fn encode_u16() { let tests = vec![ @@ -835,15 +836,17 @@ mod tests { run_encode_tests(tests); } - //#[test] - //fn encode_vector_u8() { - //let tests = vec![ - //ETestPair(vec![], vec![0xc0]), - //ETestPair(vec![15u8], vec![0xc1, 0x0f]), - //ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), - //]; - //run_encode_tests(tests); - //} + /// Vec is treated as a single value + #[test] + fn encode_vector_u8() { + let tests = vec![ + ETestPair(vec![], vec![0x80]), + ETestPair(vec![0u8], vec![0]), + ETestPair(vec![0x15], vec![0x15]), + ETestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]), + ]; + run_encode_tests(tests); + } #[test] fn encode_vector_u64() { @@ -890,28 +893,24 @@ mod tests { struct DTestPair(T, Vec) where T: rlp::Decodable + fmt::Debug + cmp::Eq; - fn run_decode_untrusted_tests(tests: Vec>) - where T: rlp::Decodable + fmt::Debug + cmp::Eq - { + fn run_decode_tests(tests: Vec>) where T: rlp::Decodable + fmt::Debug + cmp::Eq { for t in &tests { let res: T = rlp::decode_untrusted(&t.1).unwrap(); assert_eq!(res, t.0); } } - //#[test] - //fn decode_untrusted_u8() { - //let tests = vec![ - //DTestPair(0u8, vec![0u8]), - //DTestPair(15, vec![15]), - //DTestPair(55, vec![55]), - //DTestPair(56, vec![56]), - //DTestPair(0x7f, vec![0x7f]), - //DTestPair(0x80, vec![0x81, 0x80]), - //DTestPair(0xff, vec![0x81, 0xff]), - //]; - //run_decode_untrusted_tests(tests); - //} + /// Vec is treated as a single value + #[test] + fn decode_vector_u8() { + let tests = vec![ + DTestPair(vec![], vec![0x80]), + DTestPair(vec![0u8], vec![0]), + DTestPair(vec![0x15], vec![0x15]), + DTestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]), + ]; + run_decode_tests(tests); + } #[test] fn decode_untrusted_u16() { @@ -920,7 +919,7 @@ mod tests { DTestPair(0x100, vec![0x82, 0x01, 0x00]), DTestPair(0xffff, vec![0x82, 0xff, 0xff]), ]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] @@ -930,7 +929,7 @@ mod tests { DTestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]), DTestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]), ]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] @@ -940,7 +939,7 @@ mod tests { DTestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]), DTestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]), ]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] @@ -956,7 +955,7 @@ mod tests { 0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] @@ -975,7 +974,7 @@ mod tests { b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i', b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ', b'e', b'l', b'i', b't'])]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] @@ -988,19 +987,9 @@ mod tests { 0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46, 0xb3, 0x7d, 0x11, 0x06]) ]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } - //#[test] - //fn decode_untrusted_vector_u8() { - //let tests = vec![ - //DTestPair(vec![] as Vec, vec![0xc0]), - //DTestPair(vec![15u8], vec![0xc1, 0x0f]), - //DTestPair(vec![1u8, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), - //]; - //run_decode_untrusted_tests(tests); - //} - #[test] fn decode_untrusted_vector_u64() { let tests = vec![ @@ -1009,20 +998,20 @@ mod tests { DTestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), DTestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]), ]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] fn decode_untrusted_vector_str() { let tests = vec![DTestPair(vec!["cat".to_string(), "dog".to_string()], vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } #[test] fn decode_untrusted_vector_of_vectors_str() { let tests = vec![DTestPair(vec![vec!["cat".to_string()]], vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])]; - run_decode_untrusted_tests(tests); + run_decode_tests(tests); } } From f4292134c0872274e34578707f3a3ba5cb9aac1f Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 29 Nov 2015 19:42:03 +0100 Subject: [PATCH 3/3] uncommented triehash --- src/triehash.rs | 521 ++++++++++++++++++++++++------------------------ 1 file changed, 260 insertions(+), 261 deletions(-) diff --git a/src/triehash.rs b/src/triehash.rs index 6f9490cc1..ad553cf70 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -1,300 +1,299 @@ //! Generete trie root -//use std::collections::BTreeMap; -//use std::cmp; -//use hash::*; -//use sha3::*; -//use rlp; -//use rlp::RlpStream; -//use vector::SharedPrefix; +use std::collections::BTreeMap; +use std::cmp; +use hash::*; +use sha3::*; +use rlp; +use rlp::RlpStream; +use vector::SharedPrefix; -///// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. -///// -///// The "termination marker" and "leaf-node" specifier are completely equivalent. -///// -///// Input values are in range `[0, 0xf]`. -///// -///// ```markdown -///// [0,0,1,2,3,4,5] 0x10012345 // 7 > 4 -///// [0,1,2,3,4,5] 0x00012345 // 6 > 4 -///// [1,2,3,4,5] 0x112345 // 5 > 3 -///// [0,0,1,2,3,4] 0x00001234 // 6 > 3 -///// [0,1,2,3,4] 0x101234 // 5 > 3 -///// [1,2,3,4] 0x001234 // 4 > 3 -///// [0,0,1,2,3,4,5,T] 0x30012345 // 7 > 4 -///// [0,0,1,2,3,4,T] 0x20001234 // 6 > 4 -///// [0,1,2,3,4,5,T] 0x20012345 // 6 > 4 -///// [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 { - //let inlen = nibbles.len(); - //let oddness_factor = inlen % 2; - //// next even number divided by two - //let reslen = (inlen + 2) >> 1; - //let mut res = vec![]; - //res.reserve(reslen); +/// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. +/// +/// The "termination marker" and "leaf-node" specifier are completely equivalent. +/// +/// Input values are in range `[0, 0xf]`. +/// +/// ```markdown +/// [0,0,1,2,3,4,5] 0x10012345 // 7 > 4 +/// [0,1,2,3,4,5] 0x00012345 // 6 > 4 +/// [1,2,3,4,5] 0x112345 // 5 > 3 +/// [0,0,1,2,3,4] 0x00001234 // 6 > 3 +/// [0,1,2,3,4] 0x101234 // 5 > 3 +/// [1,2,3,4] 0x001234 // 4 > 3 +/// [0,0,1,2,3,4,5,T] 0x30012345 // 7 > 4 +/// [0,0,1,2,3,4,T] 0x20001234 // 6 > 4 +/// [0,1,2,3,4,5,T] 0x20012345 // 6 > 4 +/// [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 { + let inlen = nibbles.len(); + let oddness_factor = inlen % 2; + // next even number divided by two + let reslen = (inlen + 2) >> 1; + let mut res = vec![]; + res.reserve(reslen); - //let first_byte = { - //let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; - //if oddness_factor == 1 { - //bits += nibbles[0]; - //} - //bits - //}; + let first_byte = { + let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; + if oddness_factor == 1 { + bits += nibbles[0]; + } + bits + }; - //res.push(first_byte); + res.push(first_byte); - //let mut offset = oddness_factor; - //while offset < inlen { - //let byte = (nibbles[offset] << 4) + nibbles[offset + 1]; - //res.push(byte); - //offset += 2; - //} + let mut offset = oddness_factor; + while offset < inlen { + let byte = (nibbles[offset] << 4) + nibbles[offset + 1]; + res.push(byte); + offset += 2; + } - //res -//} + res +} -///// 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 { - //let mut res = vec![]; - //res.reserve(bytes.len() * 2); - //for i in 0..bytes.len() { - //res.push(bytes[i] >> 4); - //res.push((bytes[i] << 4) >> 4); - //} - //res -//} +/// 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 { + let mut res = vec![]; + res.reserve(bytes.len() * 2); + for i in 0..bytes.len() { + res.push(bytes[i] >> 4); + res.push((bytes[i] << 4) >> 4); + } + res +} -//#[derive(Debug)] -//pub struct NibblePair { - //nibble: Vec, - //data: Vec -//} +#[derive(Debug)] +pub struct NibblePair { + nibble: Vec, + data: Vec +} -//impl NibblePair { - //pub fn new(nibble: Vec, data: Vec) -> NibblePair { - //NibblePair { - //nibble: nibble, - //data: data - //} - //} +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) - //} -//} + pub fn new_raw(to_nibble: Vec, data: Vec) -> NibblePair { + NibblePair::new(as_nibbles(&to_nibble), data) + } +} -//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(); +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) -//} + 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().unwrap() - //} - //}; +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().unwrap() + } + }; - //println!("out: {:?}", out); - //out.sha3() -//} + println!("out: {:?}", out); + out.sha3() +} -//fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { - //match vec.len() { - //0 => { - //stream.append(&""); - //}, - //1 => { - //stream.append_list(2); - //stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true)); - //stream.append(&vec[0].data); - //}, - //_ => { - //let shared_prefix = vec.iter() - //// skip first element - //.skip(1) - //// get minimum number of shared nibbles between first and each successive - //.fold(usize::max_value(), | acc, pair | { - //cmp::min(vec[0].nibble.shared_prefix_len(&pair.nibble), acc) - //}); +fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { + match vec.len() { + 0 => { + stream.append(&""); + }, + 1 => { + stream.append_list(2); + stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true)); + stream.append(&vec[0].data); + }, + _ => { + let shared_prefix = vec.iter() + // skip first element + .skip(1) + // get minimum number of shared nibbles between first and each successive + .fold(usize::max_value(), | acc, pair | { + cmp::min(vec[0].nibble.shared_prefix_len(&pair.nibble), acc) + }); - //match shared_prefix > pre_len { - //true => { - //stream.append_list(2); - //stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false)); - //hash256aux(vec, shared_prefix, stream); - //}, - //false => { - //stream.append_list(17); + match shared_prefix > pre_len { + true => { + stream.append_list(2); + stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false)); + hash256aux(vec, shared_prefix, stream); + }, + false => { + stream.append_list(17); - //// every nibble is longer then previous one - //let iter = vec.iter() - //// move to first element with len different then pre_len - //.take_while(| pair | { pair.nibble.len() == pre_len }); + // every nibble is longer then previous one + let iter = vec.iter() + // move to first element with len different then pre_len + .take_while(| pair | { pair.nibble.len() == pre_len }); - //let mut begin = iter.count(); + let mut begin = iter.count(); - //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] ) - //.take_while(|&q| q == i).count(); + 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] ) + .take_while(|&q| q == i).count(); - //match len { - //0 => { stream.append(&""); }, - //_ => hash256aux(&vec[begin..begin + len], pre_len + 1, stream) - //} - //begin += len; - //} + match len { + 0 => { stream.append(&""); }, + _ => hash256aux(&vec[begin..begin + len], pre_len + 1, stream) + } + begin += len; + } - //match pre_len == vec[0].nibble.len() { - //true => stream.append(&vec[0].data), - //false => stream.append(&"") - //}; - //} - //} - //} - //}; -//} + match pre_len == vec[0].nibble.len() { + true => stream.append(&vec[0].data), + false => stream.append(&"") + }; + } + } + } + }; +} -//fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { - //let mut s = RlpStream::new(); - //hash256rlp(vec, pre_len, &mut s); - //let out = s.out().unwrap(); - //match out.len() { - //0...31 => stream.append_raw(&out, 1), - //_ => stream.append(&out.sha3()) - //}; -//} +fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { + let mut s = RlpStream::new(); + hash256rlp(vec, pre_len, &mut s); + let out = s.out().unwrap(); + match out.len() { + 0...31 => stream.append_raw(&out, 1), + _ => stream.append(&out.sha3()) + }; +} -//#[cfg(test)] -//mod tests { - //use std::str::FromStr; - //use rustc_serialize::hex::FromHex; - //use hash::*; - //use triehash::*; +#[cfg(test)] +mod tests { + use std::str::FromStr; + use rustc_serialize::hex::FromHex; + use hash::*; + use triehash::*; - //#[test] - //fn empty_trie_root() { - //assert_eq!(hash256(&vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); - //} + #[test] + fn empty_trie_root() { + assert_eq!(hash256(&vec![]), H256::from_str("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").unwrap()); + } - //#[test] - //fn single_trie_item() { + #[test] + fn single_trie_item() { - //let v = vec![ - //NibblePair::new_raw(From::from("A"), - //From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - //]; - //println!("{:?}", v); - //assert_eq!(hash256(&v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); - //} + let v = vec![ + NibblePair::new_raw(From::from("A"), + From::from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) + ]; + + assert_eq!(hash256(&v), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); + } - //#[test] - //fn test_trie_root() { - //let v = vec![ - //NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), - //"22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), + #[test] + fn test_trie_root() { + let _v = vec![ + NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000045".from_hex().unwrap(), + "22b224a1420a802ab51d326e29fa98e34c4f24ea".from_hex().unwrap()), - //NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), - //"67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + NibblePair::new_raw("0000000000000000000000000000000000000000000000000000000000000046".from_hex().unwrap(), + "67706c2076330000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - //NibblePair::new_raw("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), - //"6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), + NibblePair::new_raw("000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6".from_hex().unwrap(), + "6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000".from_hex().unwrap()), - //NibblePair::new_raw("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), - //"4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), + NibblePair::new_raw("0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap(), + "4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap()), - //NibblePair::new_raw("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), - //"4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), + NibblePair::new_raw("000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap(), + "4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap()), - //NibblePair::new_raw("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), - //"7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), + NibblePair::new_raw("4655474156000000000000000000000000000000000000000000000000000000".from_hex().unwrap(), + "7ef9e639e2733cb34e4dfc576d4b23f72db776b2".from_hex().unwrap()), - //NibblePair::new_raw("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), - //"ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()), + NibblePair::new_raw("4e616d6552656700000000000000000000000000000000000000000000000000".from_hex().unwrap(), + "ec4f34c97e43fbb2816cfd95e388353c7181dab1".from_hex().unwrap()), - //NibblePair::new_raw("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 _root = H256::from_str("9f6221ebb8efe7cff60a716ecb886e67dd042014be444669f0159d8e68b42100").unwrap(); //let res = hash256(&v); - //println!("{:?}", res); - ////assert!(false); - //} -//} + //assert_eq!(res, root); + } +}