diff --git a/src/bloom.rs b/src/bloom.rs new file mode 100644 index 000000000..fe619fae6 --- /dev/null +++ b/src/bloom.rs @@ -0,0 +1,7 @@ +use bytes::BytesConvertable; +// use hash::FixedHash; + +pub trait Bloomable { + fn shift_bloom(&mut self, bytes: &T) where T: BytesConvertable; + fn contains_bloom(&self, bytes: &T) -> bool where T: BytesConvertable; +} diff --git a/src/bytes.rs b/src/bytes.rs index 91f9fcda2..9521abdbd 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -34,7 +34,7 @@ macro_rules! impl_bytes_convertable_for_array { } } -// two -1 at the end is not expanded +// -1 at the end is not expanded impl_bytes_convertable_for_array! { 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1 diff --git a/src/hash.rs b/src/hash.rs index 044153e0c..98a76ad87 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -1,34 +1,29 @@ use std::str::FromStr; use std::fmt; use std::hash::{Hash, Hasher}; -use std::ops::{Index, IndexMut}; +use std::ops::{Index, IndexMut, BitOr}; use rustc_serialize::hex::*; use error::EthcoreError; use rand::Rng; use rand::os::OsRng; use bytes::BytesConvertable; +/// types implementing FixedHash must be also BytesConvertable +pub trait FixedHash: BytesConvertable { + fn random() -> Self; + fn randomize(&mut self); + fn mut_bytes(&mut self) -> &mut [u8]; +} + macro_rules! impl_hash { ($from: ident, $size: expr) => { #[derive(Eq)] pub struct $from (pub [u8; $size]); - impl $from { - pub fn new() -> $from { - $from([0; $size]) - } - pub fn random() -> $from { - let mut hash = $from::new(); - hash.randomize(); - hash - } - pub fn randomize(&mut self) { - let mut rng = OsRng::new().unwrap(); - rng.fill_bytes(&mut self.0); - } - pub fn mut_bytes(&mut self) -> &mut [u8; $size] { - &mut self.0 + impl $from { + fn new() -> $from { + $from([0; $size]) } } @@ -38,6 +33,23 @@ macro_rules! impl_hash { } } + impl FixedHash for $from { + fn random() -> $from { + let mut hash = $from::new(); + hash.randomize(); + hash + } + + fn randomize(&mut self) { + let mut rng = OsRng::new().unwrap(); + rng.fill_bytes(&mut self.0); + } + + fn mut_bytes(&mut self) -> &mut [u8] { + &mut self.0 + } + } + impl FromStr for $from { type Err = EthcoreError; @@ -68,10 +80,14 @@ macro_rules! impl_hash { impl Clone for $from { fn clone(&self) -> $from { - *self + unsafe { + use std::{mem, ptr}; + let mut ret: $from = mem::uninitialized(); + ptr::copy(self.0.as_ptr(), ret.0.as_mut_ptr(), mem::size_of::<$from>()); + ret + } } } - impl Copy for $from {} impl PartialEq for $from { fn eq(&self, other: &Self) -> bool { @@ -103,6 +119,30 @@ macro_rules! impl_hash { &mut self.0[index] } } + + impl<'a> BitOr for &'a $from { + type Output = $from; + + fn bitor(self, rhs: Self) -> Self::Output { + unsafe { + use std::mem; + let mut ret: $from = mem::uninitialized(); + for i in 0..$size { + ret.0[i] = self.0[i] | rhs.0[i]; + } + ret + } + } + } + + impl BitOr for $from { + type Output = $from; + + fn bitor(self, rhs: Self) -> Self::Output { + &self | &rhs + } + } + } } @@ -114,7 +154,6 @@ impl_hash!(H512, 64); impl_hash!(H520, 65); impl_hash!(H1024, 128); impl_hash!(H2048, 256); -impl_hash!(H4096, 512); #[test] fn hash() { @@ -126,3 +165,16 @@ fn hash() { assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); assert!(h != H64([0; 8])); } + +#[test] +fn hash_bitor() { + let a = H64([1; 8]); + let b = H64([2; 8]); + let c = H64([3; 8]); + + // borrow + assert_eq!(&a | &b, c); + + // move + assert_eq!(a | b, c); +} diff --git a/src/lib.rs b/src/lib.rs index a43585e0c..f7f446ddc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod db; pub mod sha3; pub mod hashdb; pub mod memorydb; +pub mod bloom; //pub mod network; diff --git a/src/rlp.rs b/src/rlp.rs index f70062457..ee74c3a33 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -2,13 +2,13 @@ //! //! Types implementing `Endocable` and `Decodable` traits //! can be easily coverted to and from rlp -//! +//! //! # Examples: -//! +//! //! ```rust //! extern crate ethcore_util; //! use ethcore_util::rlp::{RlpStream}; -//! +//! //! fn encode_value() { //! // 1029 //! let mut stream = RlpStream::new(); @@ -24,7 +24,7 @@ //! let out = stream.out().unwrap(); //! assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); //! } -//! +//! //! fn encode_list2() { //! // [ [], [[]], [ [], [[]] ] ] //! let mut stream = RlpStream::new_list(3); @@ -52,552 +52,591 @@ use vector::InsertSlice; /// rlp container #[derive(Debug)] -pub struct Rlp<'a>{ - bytes: &'a [u8], - cache: Cell +pub struct Rlp<'a> { + bytes: &'a [u8], + cache: Cell, } /// rlp offset #[derive(Copy, Clone, Debug)] struct OffsetCache { - index: usize, - offset: usize + index: usize, + offset: usize, } impl OffsetCache { - fn new(index: usize, offset: usize) -> OffsetCache { - OffsetCache { index: index, offset: offset } - } + fn new(index: usize, offset: usize) -> OffsetCache { + OffsetCache { + index: index, + offset: offset, + } + } } /// stores basic information about item struct ItemInfo { - prefix_len: usize, - value_len: usize + prefix_len: usize, + value_len: usize, } impl ItemInfo { - fn new(prefix_len: usize, value_len: usize) -> ItemInfo { - ItemInfo { prefix_len: prefix_len, value_len: value_len } - } + fn new(prefix_len: usize, value_len: usize) -> ItemInfo { + ItemInfo { + prefix_len: prefix_len, + value_len: value_len, + } + } } #[derive(Debug, PartialEq, Eq)] pub enum DecoderError { - FromBytesError(FromBytesError), - RlpIsTooShort, - RlpExpectedToBeList, - RlpExpectedToBeValue, - BadRlp, + FromBytesError(FromBytesError), + RlpIsTooShort, + RlpExpectedToBeList, + RlpExpectedToBeValue, + BadRlp, } impl StdError for DecoderError { - fn description(&self) -> &str { "builder error" } + fn description(&self) -> &str { + "builder error" + } } impl fmt::Display for DecoderError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self, f) - } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self, f) + } } impl From for DecoderError { - fn from(err: FromBytesError) -> DecoderError { DecoderError::FromBytesError(err) } + fn from(err: FromBytesError) -> DecoderError { + DecoderError::FromBytesError(err) + } } -impl <'a>Rlp<'a> { - /// returns new instance of `Rlp` - pub fn new(bytes: &'a[u8]) -> Rlp<'a> { - Rlp { - bytes: bytes, - cache: Cell::new(OffsetCache::new(usize::max_value(), 0)) - } - } +impl<'a> Rlp<'a> { + /// returns new instance of `Rlp` + pub fn new(bytes: &'a [u8]) -> Rlp<'a> { + Rlp { + bytes: bytes, + cache: Cell::new(OffsetCache::new(usize::max_value(), 0)), + } + } - /// get container subset at given index - /// - /// paren container caches searched position - pub fn at(&self, index: usize) -> Result, DecoderError> { - if !self.is_list() { - return Err(DecoderError::RlpExpectedToBeList); - } + /// get container subset at given index + /// + /// paren container caches searched position + pub fn at(&self, index: usize) -> Result, DecoderError> { + if !self.is_list() { + return Err(DecoderError::RlpExpectedToBeList); + } - // move to cached position if it's index is less or equal to - // current search index, otherwise move to beginning of list - let c = self.cache.get(); - let (mut bytes, to_skip) = match c.index <= index { - true => (try!(Rlp::consume(self.bytes, c.offset)), index - c.index), - false => (try!(self.consume_list_prefix()), index) - }; + // move to cached position if it's index is less or equal to + // current search index, otherwise move to beginning of list + let c = self.cache.get(); + let (mut bytes, to_skip) = match c.index <= index { + true => (try!(Rlp::consume(self.bytes, c.offset)), index - c.index), + false => (try!(self.consume_list_prefix()), index), + }; - // skip up to x items - bytes = try!(Rlp::consume_items(bytes, to_skip)); + // skip up to x items + bytes = try!(Rlp::consume_items(bytes, to_skip)); - // update the cache - self.cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len())); + // update the cache + self.cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len())); - // construct new rlp - let found = try!(Rlp::item_info(bytes)); - Ok(Rlp::new(&bytes[0..found.prefix_len + found.value_len])) - } + // construct new rlp + let found = try!(Rlp::item_info(bytes)); + Ok(Rlp::new(&bytes[0..found.prefix_len + found.value_len])) + } - /// returns true if rlp is a list - pub fn is_list(&self) -> bool { - self.bytes.len() > 0 && self.bytes[0] >= 0xc0 - } + /// returns true if rlp is a list + pub fn is_list(&self) -> bool { + self.bytes.len() > 0 && self.bytes[0] >= 0xc0 + } - /// returns true if rlp is a value - pub fn is_value(&self) -> bool { - self.bytes.len() > 0 && self.bytes[0] <= 0xbf - } + /// returns true if rlp is a value + pub fn is_value(&self) -> bool { + self.bytes.len() > 0 && self.bytes[0] <= 0xbf + } - /// returns rlp iterator - pub fn iter(&'a self) -> RlpIterator<'a> { - self.into_iter() - } + /// returns rlp iterator + pub fn iter(&'a self) -> RlpIterator<'a> { + self.into_iter() + } - /// consumes first found prefix - fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> { - let item = try!(Rlp::item_info(self.bytes)); - let bytes = try!(Rlp::consume(self.bytes, item.prefix_len)); - Ok(bytes) - } + /// consumes first found prefix + fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> { + let item = try!(Rlp::item_info(self.bytes)); + let bytes = try!(Rlp::consume(self.bytes, item.prefix_len)); + Ok(bytes) + } - /// consumes fixed number of items - fn consume_items(bytes: &'a [u8], items: usize) -> Result<&'a [u8], DecoderError> { - let mut result = bytes; - for _ in 0..items { - let i = try!(Rlp::item_info(result)); - result = try!(Rlp::consume(result, (i.prefix_len + i.value_len))); - } - Ok(result) - } + /// consumes fixed number of items + fn consume_items(bytes: &'a [u8], items: usize) -> Result<&'a [u8], DecoderError> { + let mut result = bytes; + for _ in 0..items { + let i = try!(Rlp::item_info(result)); + result = try!(Rlp::consume(result, (i.prefix_len + i.value_len))); + } + Ok(result) + } - /// return first item info - fn item_info(bytes: &[u8]) -> Result { - let item = match bytes.first().map(|&x| x) { - None => return Err(DecoderError::RlpIsTooShort), - Some(0...0x7f) => ItemInfo::new(0, 1), - Some(l @ 0x80...0xb7) => ItemInfo::new(1, l as usize - 0x80), - Some(l @ 0xb8...0xbf) => { - let len_of_len = l as usize - 0xb7; - let prefix_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); - ItemInfo::new(prefix_len, value_len) - } - Some(l @ 0xc0...0xf7) => ItemInfo::new(1, l as usize - 0xc0), - Some(l @ 0xf8...0xff) => { - let len_of_len = l as usize - 0xf7; - let prefix_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); - ItemInfo::new(prefix_len, value_len) - }, - _ => return Err(DecoderError::BadRlp) - }; + /// return first item info + fn item_info(bytes: &[u8]) -> Result { + let item = match bytes.first().map(|&x| x) { + None => return Err(DecoderError::RlpIsTooShort), + Some(0...0x7f) => ItemInfo::new(0, 1), + Some(l @ 0x80...0xb7) => ItemInfo::new(1, l as usize - 0x80), + Some(l @ 0xb8...0xbf) => { + let len_of_len = l as usize - 0xb7; + let prefix_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); + ItemInfo::new(prefix_len, value_len) + } + Some(l @ 0xc0...0xf7) => ItemInfo::new(1, l as usize - 0xc0), + Some(l @ 0xf8...0xff) => { + let len_of_len = l as usize - 0xf7; + let prefix_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); + ItemInfo::new(prefix_len, value_len) + } + _ => return Err(DecoderError::BadRlp), + }; - match item.prefix_len + item.value_len <= bytes.len() { - true => Ok(item), - false => Err(DecoderError::RlpIsTooShort) - } - } + match item.prefix_len + item.value_len <= bytes.len() { + true => Ok(item), + false => Err(DecoderError::RlpIsTooShort), + } + } - /// consumes slice prefix of length `len` - fn consume(bytes: &'a [u8], len: usize) -> Result<&'a [u8], DecoderError> { - match bytes.len() >= len { - true => Ok(&bytes[len..]), - false => Err(DecoderError::RlpIsTooShort) - } - } + /// consumes slice prefix of length `len` + fn consume(bytes: &'a [u8], len: usize) -> Result<&'a [u8], DecoderError> { + match bytes.len() >= len { + true => Ok(&bytes[len..]), + false => Err(DecoderError::RlpIsTooShort), + } + } } /// non-consuming rlp iterator pub struct RlpIterator<'a> { - rlp: &'a Rlp<'a>, - index: usize + rlp: &'a Rlp<'a>, + index: usize, } -impl <'a> IntoIterator for &'a Rlp<'a> { - type Item = Rlp<'a>; +impl<'a> IntoIterator for &'a Rlp<'a> { + type Item = Rlp<'a>; type IntoIter = RlpIterator<'a>; - fn into_iter(self) -> Self::IntoIter { - RlpIterator { rlp: self, index: 0 } - } + fn into_iter(self) -> Self::IntoIter { + RlpIterator { + rlp: self, + index: 0, + } + } } -impl <'a> Iterator for RlpIterator<'a> { - type Item = Rlp<'a>; +impl<'a> Iterator for RlpIterator<'a> { + type Item = Rlp<'a>; - fn next(&mut self) -> Option> { - let index = self.index; - let result = self.rlp.at(index).ok(); - self.index += 1; - result - } + fn next(&mut self) -> Option> { + let index = self.index; + let result = self.rlp.at(index).ok(); + self.index += 1; + result + } } /// shortcut function to decode a Rlp `&[u8]` into an object -pub fn decode(bytes: &[u8]) -> Result where T: Decodable { - let rlp = Rlp::new(bytes); - T::decode(&rlp) +pub fn decode(bytes: &[u8]) -> Result + where T: Decodable +{ + let rlp = Rlp::new(bytes); + T::decode(&rlp) } pub trait Decodable: Sized { - fn decode(rlp: &Rlp) -> Result; + fn decode(rlp: &Rlp) -> Result; } -impl Decodable for T where T: FromBytes { - fn decode(rlp: &Rlp) -> Result { - match rlp.is_value() { - true => BasicDecoder::read_value(rlp.bytes), - false => Err(DecoderError::RlpExpectedToBeValue) - } - } +impl Decodable for T where T: FromBytes +{ + fn decode(rlp: &Rlp) -> Result { + match rlp.is_value() { + true => BasicDecoder::read_value(rlp.bytes), + false => Err(DecoderError::RlpExpectedToBeValue), + } + } } -impl Decodable for Vec where T: Decodable { - fn decode(rlp: &Rlp) -> Result { - match rlp.is_list() { - true => rlp.iter().map(|rlp| T::decode(&rlp)).collect(), - false => Err(DecoderError::RlpExpectedToBeList) - } - } +impl Decodable for Vec where T: Decodable +{ + fn decode(rlp: &Rlp) -> Result { + match rlp.is_list() { + true => rlp.iter().map(|rlp| T::decode(&rlp)).collect(), + false => Err(DecoderError::RlpExpectedToBeList), + } + } } pub trait Decoder { - fn read_value(bytes: &[u8]) -> Result where T: FromBytes; + fn read_value(bytes: &[u8]) -> Result where T: FromBytes; } struct BasicDecoder; impl Decoder for BasicDecoder { - fn read_value(bytes: &[u8]) -> Result where T: FromBytes { - match bytes.first().map(|&x| x) { - // rlp is too short - None => Err(DecoderError::RlpIsTooShort), - // single byt value - Some(l @ 0...0x7f) => Ok(try!(T::from_bytes(&[l]))), - // 0-55 bytes - Some(l @ 0x80...0xb7) => Ok(try!(T::from_bytes(&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]))) - }, - _ => Err(DecoderError::BadRlp) - } - } + fn read_value(bytes: &[u8]) -> Result + where T: FromBytes + { + match bytes.first().map(|&x| x) { + // rlp is too short + None => Err(DecoderError::RlpIsTooShort), + // single byt value + Some(l @ 0...0x7f) => Ok(try!(T::from_bytes(&[l]))), + // 0-55 bytes + Some(l @ 0x80...0xb7) => Ok(try!(T::from_bytes(&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]))) + } + _ => Err(DecoderError::BadRlp), + } + } } #[derive(Debug)] struct ListInfo { - position: usize, - current: usize, - max: usize + position: usize, + current: usize, + max: usize, } impl ListInfo { - fn new(position: usize, max: usize) -> ListInfo { - ListInfo { - position: position, - current: 0, - max: max - } - } + fn new(position: usize, max: usize) -> ListInfo { + ListInfo { + position: position, + current: 0, + max: max, + } + } } /// container that should be used to encode rlp pub struct RlpStream { - unfinished_lists: LinkedList, - encoder: BasicEncoder + unfinished_lists: LinkedList, + encoder: BasicEncoder, } impl RlpStream { - /// create new container for values appended one after another, - /// but not being part of the same list - pub fn new() -> RlpStream { - RlpStream { - unfinished_lists: LinkedList::new(), - encoder: BasicEncoder::new() - } - } + /// create new container for values appended one after another, + /// but not being part of the same list + pub fn new() -> RlpStream { + RlpStream { + unfinished_lists: LinkedList::new(), + encoder: BasicEncoder::new(), + } + } - /// create new container for list of size `max_len` - pub fn new_list(len: usize) -> RlpStream { - let mut stream = RlpStream::new(); - stream.append_list(len); - stream - } + /// create new container for list of size `max_len` + pub fn new_list(len: usize) -> RlpStream { + let mut stream = RlpStream::new(); + stream.append_list(len); + stream + } - /// apends value to the end of stream, chainable - pub fn append<'a, E>(&'a mut self, object: &E) -> &'a mut RlpStream where E: Encodable { - // encode given value and add it at the end of the stream - object.encode(&mut self.encoder); + /// apends value to the end of stream, chainable + pub fn append<'a, E>(&'a mut self, object: &E) -> &'a mut RlpStream + where E: Encodable + { + // encode given value and add it at the end of the stream + object.encode(&mut self.encoder); - // if list is finished, prepend the length - self.try_to_finish(); + // if list is finished, prepend the length + self.try_to_finish(); - // return chainable self - self - } + // return chainable self + self + } - /// declare appending the list of given size - pub fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream { - // push new list - let position = self.encoder.bytes.len(); - match len { - 0 => { - // we may finish, if the appended list len is equal 0 - self.encoder.bytes.push(0xc0u8); - self.try_to_finish(); - }, - _ => self.unfinished_lists.push_back(ListInfo::new(position, len)) - } + /// declare appending the list of given size + pub fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream { + // push new list + let position = self.encoder.bytes.len(); + match len { + 0 => { + // we may finish, if the appended list len is equal 0 + self.encoder.bytes.push(0xc0u8); + self.try_to_finish(); + } + _ => self.unfinished_lists.push_back(ListInfo::new(position, len)), + } - // return chainable self - self - } + // return chainable self + self + } - /// return true if stream is ready - pub fn is_finished(&self) -> bool { - self.unfinished_lists.back().is_none() - } + /// return true if stream is ready + pub fn is_finished(&self) -> bool { + self.unfinished_lists.back().is_none() + } - /// streams out encoded bytes - pub fn out(self) -> Result, EncoderError> { - match self.is_finished() { - true => Ok(self.encoder.out()), - false => Err(EncoderError::StreamIsUnfinished) - } - } + /// streams out encoded bytes + pub fn out(self) -> Result, EncoderError> { + match self.is_finished() { + true => Ok(self.encoder.out()), + false => Err(EncoderError::StreamIsUnfinished), + } + } - /// try to finish lists - fn try_to_finish(&mut self) -> () { - let should_finish = match self.unfinished_lists.back_mut() { - None => false, - Some(ref mut x) => { - x.current += 1; - x.current == x.max - } - }; + /// try to finish lists + fn try_to_finish(&mut self) -> () { + let should_finish = match self.unfinished_lists.back_mut() { + None => false, + Some(ref mut x) => { + x.current += 1; + x.current == x.max + } + }; - if should_finish { - let x = self.unfinished_lists.pop_back().unwrap(); - let len = self.encoder.bytes.len() - x.position; - self.encoder.insert_list_len_at_pos(len, x.position); - self.try_to_finish(); - } - } + if should_finish { + let x = self.unfinished_lists.pop_back().unwrap(); + let len = self.encoder.bytes.len() - x.position; + self.encoder.insert_list_len_at_pos(len, x.position); + self.try_to_finish(); + } + } } /// shortcut function to encode a `T: Encodable` into a Rlp `Vec` -pub fn encode(object: &E) -> Vec where E: Encodable { - let mut encoder = BasicEncoder::new(); - object.encode(&mut encoder); - encoder.out() +pub fn encode(object: &E) -> Vec + where E: Encodable +{ + let mut encoder = BasicEncoder::new(); + object.encode(&mut encoder); + encoder.out() } #[derive(Debug)] pub enum EncoderError { - StreamIsUnfinished + StreamIsUnfinished, } impl StdError for EncoderError { - fn description(&self) -> &str { "encoder error" } + fn description(&self) -> &str { + "encoder error" + } } impl fmt::Display for EncoderError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self, f) - } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&self, f) + } } pub trait Encodable { - fn encode(&self, encoder: &mut E) -> () where E: Encoder; + fn encode(&self, encoder: &mut E) -> () where E: Encoder; } pub trait Encoder { - fn emit_value(&mut self, bytes: &[u8]) -> (); - fn emit_list(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> (); + fn emit_value(&mut self, bytes: &[u8]) -> (); + 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 { - encoder.emit_value(&self.to_bytes()) - } +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 { - encoder.emit_list(|e| { - // insert all list elements - for el in self.iter() { - el.encode(e); - } - }) - } +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() { + el.encode(e); + } + }) + } } -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) - } +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) + } } struct BasicEncoder { - bytes: Vec + bytes: Vec, } impl BasicEncoder { - fn new() -> BasicEncoder { - BasicEncoder { bytes: vec![] } - } + fn new() -> BasicEncoder { + BasicEncoder { bytes: vec![] } + } - /// inserts list prefix at given position - /// TODO: optimise it further? - fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () { - let mut res = vec![]; - match len { - 0...55 => res.push(0xc0u8 + len as u8), - _ => { - res.push(0x7fu8 + len.to_bytes_len() as u8); - res.extend(len.to_bytes()); - } - }; + /// inserts list prefix at given position + /// TODO: optimise it further? + fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () { + let mut res = vec![]; + match len { + 0...55 => res.push(0xc0u8 + len as u8), + _ => { + res.push(0x7fu8 + len.to_bytes_len() as u8); + res.extend(len.to_bytes()); + } + }; - self.bytes.insert_slice(pos, &res); - } + self.bytes.insert_slice(pos, &res); + } - /// get encoded value - fn out(self) -> Vec { - self.bytes - } + /// get encoded value + fn out(self) -> Vec { + self.bytes + } } impl Encoder for BasicEncoder { - fn emit_value(&mut self, bytes: &[u8]) -> () { - match bytes.len() { - // just 0 - 0 => self.bytes.push(0x80u8), - // byte is its own encoding - 1 if bytes[0] < 0x80 => self.bytes.extend(bytes), - // (prefix + length), followed by the string - len @ 1 ... 55 => { - self.bytes.push(0x80u8 + len as u8); - self.bytes.extend(bytes); - } - // (prefix + length of length), followed by the length, followd by the string - len => { - self.bytes.push(0xb7 + len.to_bytes_len() as u8); - self.bytes.extend(len.to_bytes()); - self.bytes.extend(bytes); - } - } - } + fn emit_value(&mut self, bytes: &[u8]) -> () { + match bytes.len() { + // just 0 + 0 => self.bytes.push(0x80u8), + // byte is its own encoding + 1 if bytes[0] < 0x80 => self.bytes.extend(bytes), + // (prefix + length), followed by the string + len @ 1 ... 55 => { + self.bytes.push(0x80u8 + len as u8); + self.bytes.extend(bytes); + } + // (prefix + length of length), followed by the length, followd by the string + len => { + self.bytes.push(0xb7 + len.to_bytes_len() as u8); + self.bytes.extend(len.to_bytes()); + self.bytes.extend(bytes); + } + } + } - fn emit_list(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> () { - // get len before inserting a list - let before_len = self.bytes.len(); + fn emit_list(&mut self, f: F) -> () + where F: FnOnce(&mut Self) -> () + { + // get len before inserting a list + let before_len = self.bytes.len(); - // insert all list elements - f(self); + // insert all list elements + f(self); - // get len after inserting a list - let after_len = self.bytes.len(); + // get len after inserting a list + let after_len = self.bytes.len(); - // diff is list len - let list_len = after_len - before_len; - self.insert_list_len_at_pos(list_len, before_len); - } + // diff is list len + let list_len = after_len - before_len; + self.insert_list_len_at_pos(list_len, before_len); + } } #[cfg(test)] mod tests { - use std::{fmt, cmp}; - use std::str::FromStr; - use rlp; - use rlp::{Rlp, RlpStream, Decodable}; - use uint::U256; + use std::{fmt, cmp}; + use std::str::FromStr; + use rlp; + use rlp::{Rlp, RlpStream, Decodable}; + use uint::U256; - #[test] - fn rlp_at() { - let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; - { - let rlp = Rlp::new(&data); - assert!(rlp.is_list()); - let animals = as rlp::Decodable>::decode(&rlp).unwrap(); - assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]); - - let cat = rlp.at(0).unwrap(); - assert!(cat.is_value()); - assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); - assert_eq!(String::decode(&cat).unwrap(), "cat".to_string()); - - let dog = rlp.at(1).unwrap(); - assert!(dog.is_value()); - assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); - assert_eq!(String::decode(&dog).unwrap(), "dog".to_string()); + #[test] + fn rlp_at() { + let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; + { + let rlp = Rlp::new(&data); + assert!(rlp.is_list()); + let animals = + as rlp::Decodable>::decode(&rlp).unwrap(); + assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]); - let cat_again = rlp.at(0).unwrap(); - assert!(cat_again.is_value()); - assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); - assert_eq!(String::decode(&cat_again).unwrap(), "cat".to_string()); - } - } + let cat = rlp.at(0).unwrap(); + assert!(cat.is_value()); + assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); + assert_eq!(String::decode(&cat).unwrap(), "cat".to_string()); - #[test] - fn rlp_at_err() { - let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o']; - { - let rlp = Rlp::new(&data); - assert!(rlp.is_list()); + let dog = rlp.at(1).unwrap(); + assert!(dog.is_value()); + assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); + assert_eq!(String::decode(&dog).unwrap(), "dog".to_string()); - let cat_err = rlp.at(0).unwrap_err(); - assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort); + let cat_again = rlp.at(0).unwrap(); + assert!(cat_again.is_value()); + assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); + assert_eq!(String::decode(&cat_again).unwrap(), "cat".to_string()); + } + } - let dog_err = rlp.at(1).unwrap_err(); - assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort); - } - } + #[test] + fn rlp_at_err() { + let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o']; + { + let rlp = Rlp::new(&data); + assert!(rlp.is_list()); - #[test] - fn rlp_iter() { - let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; - { - let rlp = Rlp::new(&data); - let mut iter = rlp.iter(); + let cat_err = rlp.at(0).unwrap_err(); + assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort); - let cat = iter.next().unwrap(); - assert!(cat.is_value()); - assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); + let dog_err = rlp.at(1).unwrap_err(); + assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort); + } + } - let dog = iter.next().unwrap(); - assert!(dog.is_value()); - assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); + #[test] + fn rlp_iter() { + let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; + { + let rlp = Rlp::new(&data); + let mut iter = rlp.iter(); - let none = iter.next(); - assert!(none.is_none()); + let cat = iter.next().unwrap(); + assert!(cat.is_value()); + assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']); - let cat_again = rlp.at(0).unwrap(); - assert!(cat_again.is_value()); - assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); - } - } + let dog = iter.next().unwrap(); + assert!(dog.is_value()); + assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']); - struct ETestPair(T, Vec) where T: rlp::Encodable; + let none = iter.next(); + assert!(none.is_none()); - fn run_encode_tests(tests: Vec>) where T: rlp::Encodable { - for t in &tests { - let res = rlp::encode(&t.0); - assert_eq!(res, &t.1[..]); - } - } + let cat_again = rlp.at(0).unwrap(); + assert!(cat_again.is_value()); + assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']); + } + } - #[test] - fn encode_u8() { - let tests = vec![ + struct ETestPair(T, Vec) where T: rlp::Encodable; + + fn run_encode_tests(tests: Vec>) + where T: rlp::Encodable + { + for t in &tests { + let res = rlp::encode(&t.0); + assert_eq!(res, &t.1[..]); + } + } + + #[test] + fn encode_u8() { + let tests = vec![ ETestPair(0u8, vec![0x80u8]), ETestPair(15, vec![15]), ETestPair(55, vec![55]), @@ -606,139 +645,139 @@ mod tests { ETestPair(0x80, vec![0x81, 0x80]), ETestPair(0xff, vec![0x81, 0xff]), ]; - run_encode_tests(tests); - } + run_encode_tests(tests); + } - #[test] - fn encode_u16() { - let tests = vec![ + #[test] + fn encode_u16() { + let tests = vec![ ETestPair(0u16, vec![0x80u8]), ETestPair(0x100, vec![0x82, 0x01, 0x00]), ETestPair(0xffff, vec![0x82, 0xff, 0xff]), ]; - run_encode_tests(tests); - } + run_encode_tests(tests); + } - #[test] - fn encode_u32() { - let tests = vec![ + #[test] + fn encode_u32() { + let tests = vec![ ETestPair(0u32, vec![0x80u8]), ETestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]), ETestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]), ]; - run_encode_tests(tests); - } + run_encode_tests(tests); + } - #[test] - fn encode_u64() { - let tests = vec![ + #[test] + fn encode_u64() { + let tests = vec![ ETestPair(0u64, vec![0x80u8]), ETestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]), ETestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]), ]; - run_encode_tests(tests); - } + run_encode_tests(tests); + } - #[test] - fn encode_u256() { - let tests = vec![ - ETestPair(U256::from(0u64), vec![0x80u8]), - ETestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]), - ETestPair(U256::from(0xffffffffu64), vec![0x84, 0xff, 0xff, 0xff, 0xff]), - ETestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap(), - vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, - 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_encode_tests(tests); - } + #[test] + fn encode_u256() { + let tests = vec![ETestPair(U256::from(0u64), vec![0x80u8]), + ETestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]), + ETestPair(U256::from(0xffffffffu64), + vec![0x84, 0xff, 0xff, 0xff, 0xff]), + ETestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\ + 000100000000000012f0") + .unwrap(), + vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, + 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_encode_tests(tests); + } - #[test] - fn encode_str() { - let tests = vec![ - ETestPair("cat", vec![0x83, b'c', b'a', b't']), - ETestPair("dog", vec![0x83, b'd', b'o', b'g']), - ETestPair("Marek", vec![0x85, b'M', b'a', b'r', b'e', b'k']), - ETestPair("", vec![0x80]), - ETestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit", - vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i', - b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o', b'r', - b' ', b's', b'i', b't', b' ', b'a', b'm', b'e', b't', b',', - b' ', b'c', b'o', b'n', b's', b'e', b'c', 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_encode_tests(tests); - } + #[test] + fn encode_str() { + let tests = vec![ETestPair("cat", vec![0x83, b'c', b'a', b't']), + ETestPair("dog", vec![0x83, b'd', b'o', b'g']), + ETestPair("Marek", vec![0x85, b'M', b'a', b'r', b'e', b'k']), + ETestPair("", vec![0x80]), + ETestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit", + vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i', + b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o', + b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e', + b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c', + 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_encode_tests(tests); + } - #[test] - fn encode_vector_u8() { - let tests = vec![ + #[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); - } + run_encode_tests(tests); + } - #[test] - fn encode_vector_u64() { - let tests = vec![ + #[test] + fn encode_vector_u64() { + let tests = vec![ ETestPair(vec![], vec![0xc0]), ETestPair(vec![15u64], vec![0xc1, 0x0f]), ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]), ETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]), ]; - run_encode_tests(tests); - } + run_encode_tests(tests); + } - #[test] - fn encode_vector_str() { - let tests = vec![ - ETestPair(vec!["cat", "dog"], vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']) - ]; - run_encode_tests(tests); - } + #[test] + fn encode_vector_str() { + let tests = vec![ETestPair(vec!["cat", "dog"], + vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])]; + run_encode_tests(tests); + } - #[test] - fn encode_vector_of_vectors_str() { - let tests = vec![ - ETestPair(vec![vec!["cat"]], vec![0xc5, 0xc4, 0x83, b'c', b'a', b't']) - ]; - run_encode_tests(tests); - } + #[test] + fn encode_vector_of_vectors_str() { + let tests = vec![ETestPair(vec![vec!["cat"]], vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])]; + run_encode_tests(tests); + } - #[test] - fn rlp_stream() { - let mut stream = RlpStream::new_list(2); - stream.append(&"cat").append(&"dog"); - let out = stream.out().unwrap(); - assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); - } + #[test] + fn rlp_stream() { + let mut stream = RlpStream::new_list(2); + stream.append(&"cat").append(&"dog"); + let out = stream.out().unwrap(); + assert_eq!(out, + vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); + } - #[test] - fn rlp_stream_list() { - let mut stream = RlpStream::new_list(3); - stream.append_list(0); - stream.append_list(1).append_list(0); - stream.append_list(2).append_list(0).append_list(1).append_list(0); - let out = stream.out().unwrap(); - assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]); - } + #[test] + fn rlp_stream_list() { + let mut stream = RlpStream::new_list(3); + stream.append_list(0); + stream.append_list(1).append_list(0); + stream.append_list(2).append_list(0).append_list(1).append_list(0); + let out = stream.out().unwrap(); + assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]); + } - struct DTestPair(T, Vec) where T: rlp::Decodable + fmt::Debug + cmp::Eq; + struct DTestPair(T, 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(&t.1).unwrap(); - assert_eq!(res, t.0); - } - } + fn run_decode_tests(tests: Vec>) + where T: rlp::Decodable + fmt::Debug + cmp::Eq + { + for t in &tests { + let res: T = rlp::decode(&t.1).unwrap(); + assert_eq!(res, t.0); + } + } - #[test] - fn decode_u8() { - let tests = vec![ + #[test] + fn decode_u8() { + let tests = vec![ DTestPair(0u8, vec![0u8]), DTestPair(15, vec![15]), DTestPair(55, vec![55]), @@ -747,107 +786,106 @@ mod tests { DTestPair(0x80, vec![0x81, 0x80]), DTestPair(0xff, vec![0x81, 0xff]), ]; - run_decode_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_u16() { - let tests = vec![ + #[test] + fn decode_u16() { + let tests = vec![ DTestPair(0u16, vec![0u8]), DTestPair(0x100, vec![0x82, 0x01, 0x00]), DTestPair(0xffff, vec![0x82, 0xff, 0xff]), ]; - run_decode_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_u32() { - let tests = vec![ + #[test] + fn decode_u32() { + let tests = vec![ DTestPair(0u32, vec![0u8]), DTestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]), DTestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]), ]; - run_decode_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_u64() { - let tests = vec![ + #[test] + fn decode_u64() { + let tests = vec![ DTestPair(0u64, vec![0u8]), DTestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]), DTestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]), ]; - run_decode_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_u256() { - let tests = vec![ - DTestPair(U256::from(0u64), vec![0x80u8]), - DTestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]), - DTestPair(U256::from(0xffffffffu64), vec![0x84, 0xff, 0xff, 0xff, 0xff]), - DTestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap(), - vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, - 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_tests(tests); - } + #[test] + fn decode_u256() { + let tests = vec![DTestPair(U256::from(0u64), vec![0x80u8]), + DTestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]), + DTestPair(U256::from(0xffffffffu64), + vec![0x84, 0xff, 0xff, 0xff, 0xff]), + DTestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\ + 000100000000000012f0") + .unwrap(), + vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, + 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_tests(tests); + } - #[test] - fn decode_str() { - let tests = vec![ - DTestPair("cat".to_string(), vec![0x83, b'c', b'a', b't']), - DTestPair("dog".to_string(), vec![0x83, b'd', b'o', b'g']), - DTestPair("Marek".to_string(), vec![0x85, b'M', b'a', b'r', b'e', b'k']), - DTestPair("".to_string(), vec![0x80]), - DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit".to_string(), - vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i', - b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o', b'r', - b' ', b's', b'i', b't', b' ', b'a', b'm', b'e', b't', b',', - b' ', b'c', b'o', b'n', b's', b'e', b'c', 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_tests(tests); - } - - #[test] - fn decode_vector_u8() { - let tests = vec![ + #[test] + fn decode_str() { + let tests = vec![DTestPair("cat".to_string(), vec![0x83, b'c', b'a', b't']), + DTestPair("dog".to_string(), vec![0x83, b'd', b'o', b'g']), + DTestPair("Marek".to_string(), + vec![0x85, b'M', b'a', b'r', b'e', b'k']), + DTestPair("".to_string(), vec![0x80]), + DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit" + .to_string(), + vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i', + b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o', + b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e', + b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c', + 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_tests(tests); + } + + #[test] + fn decode_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_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_vector_u64() { - let tests = vec![ + #[test] + fn decode_vector_u64() { + let tests = vec![ DTestPair(vec![], vec![0xc0]), DTestPair(vec![15u64], vec![0xc1, 0x0f]), 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_tests(tests); - } + run_decode_tests(tests); + } - #[test] - fn decode_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_tests(tests); - } + #[test] + fn decode_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_tests(tests); + } - #[test] - fn decode_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_tests(tests); - } + #[test] + fn decode_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_tests(tests); + } } - diff --git a/src/sha3.rs b/src/sha3.rs index e4ba1b007..ee328913c 100644 --- a/src/sha3.rs +++ b/src/sha3.rs @@ -1,7 +1,7 @@ use std::mem::uninitialized; use tiny_keccak::keccak_256; use bytes::BytesConvertable; -use hash::H256; +use hash::{FixedHash, H256}; pub trait Hashable { fn sha3(&self) -> H256;