Merge branch 'master' into genesis

This commit is contained in:
Paul Hauner 2018-10-24 15:26:27 +02:00
commit 28ffb037cc
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
4 changed files with 20 additions and 29 deletions

View File

@ -77,10 +77,10 @@ pub fn decode_length(bytes: &[u8], index: usize, length_bytes: usize)
return Err(DecodeError::TooShort); return Err(DecodeError::TooShort);
}; };
let mut len: usize = 0; let mut len: usize = 0;
for i in index..index+length_bytes { for (i, byte) in bytes.iter().enumerate().take(index+length_bytes).skip(index) {
let offset = (index+length_bytes - i - 1) * 8; let offset = (index+length_bytes - i - 1) * 8;
len = ((bytes[i] as usize) << offset) | len; len |= (*byte as usize) << offset;
}; }
Ok(len) Ok(len)
} }

View File

@ -1,13 +1,7 @@
use super::{ use super::{
LENGTH_BYTES, LENGTH_BYTES
MAX_LIST_SIZE,
}; };
#[derive(Debug)]
pub enum EncodeError {
ListTooLong,
}
pub trait Encodable { pub trait Encodable {
fn ssz_append(&self, s: &mut SszStream); fn ssz_append(&self, s: &mut SszStream);
} }
@ -17,6 +11,7 @@ pub trait Encodable {
/// Use the `append()` fn to add a value to a list, then use /// Use the `append()` fn to add a value to a list, then use
/// the `drain()` method to consume the struct and return the /// the `drain()` method to consume the struct and return the
/// ssz encoded bytes. /// ssz encoded bytes.
#[derive(Default)]
pub struct SszStream { pub struct SszStream {
buffer: Vec<u8> buffer: Vec<u8>
} }
@ -41,7 +36,7 @@ impl SszStream {
/// ///
/// The length of the supplied bytes will be concatenated /// The length of the supplied bytes will be concatenated
/// to the stream before the supplied bytes. /// to the stream before the supplied bytes.
pub fn append_encoded_val(&mut self, vec: &Vec<u8>) { pub fn append_encoded_val(&mut self, vec: &[u8]) {
self.buffer.extend_from_slice( self.buffer.extend_from_slice(
&encode_length(vec.len(), &encode_length(vec.len(),
LENGTH_BYTES)); LENGTH_BYTES));
@ -51,7 +46,7 @@ impl SszStream {
/// Append some ssz encoded bytes to the stream without calculating length /// Append some ssz encoded bytes to the stream without calculating length
/// ///
/// The raw bytes will be concatenated to the stream. /// The raw bytes will be concatenated to the stream.
pub fn append_encoded_raw(&mut self, vec: &Vec<u8>) { pub fn append_encoded_raw(&mut self, vec: &[u8]) {
self.buffer.extend_from_slice(&vec); self.buffer.extend_from_slice(&vec);
} }
@ -59,7 +54,7 @@ impl SszStream {
/// ///
/// The length of the list will be concatenated to the stream, then /// The length of the list will be concatenated to the stream, then
/// each item in the vector will be encoded and concatenated. /// each item in the vector will be encoded and concatenated.
pub fn append_vec<E>(&mut self, vec: &Vec<E>) pub fn append_vec<E>(&mut self, vec: &[E])
where E: Encodable where E: Encodable
{ {
let mut list_stream = SszStream::new(); let mut list_stream = SszStream::new();
@ -83,9 +78,9 @@ pub fn encode_length(len: usize, length_bytes: usize) -> Vec<u8> {
assert!(length_bytes > 0); // For sanity assert!(length_bytes > 0); // For sanity
assert!((len as usize) < 2usize.pow(length_bytes as u32 * 8)); assert!((len as usize) < 2usize.pow(length_bytes as u32 * 8));
let mut header: Vec<u8> = vec![0; length_bytes]; let mut header: Vec<u8> = vec![0; length_bytes];
for i in 0..length_bytes { for (i, header_byte) in header.iter_mut().enumerate() {
let offset = (length_bytes - i - 1) * 8; let offset = (length_bytes - i - 1) * 8;
header[i] = ((len >> offset) & 0xff) as u8; *header_byte = ((len >> offset) & 0xff) as u8;
}; };
header header
} }

View File

@ -19,10 +19,10 @@ macro_rules! impl_decodable_for_uint {
if bytes.len() >= (index + max_bytes) { if bytes.len() >= (index + max_bytes) {
let end_bytes = index + max_bytes; let end_bytes = index + max_bytes;
let mut result: $type = 0; let mut result: $type = 0;
for i in index..end_bytes { for (i, byte) in bytes.iter().enumerate().take(end_bytes).skip(index) {
let offset = ((index + max_bytes) - i - 1) * 8; let offset = (end_bytes - i - 1) * 8;
result = ((bytes[i] as $type) << offset) | result; result |= ($type::from(*byte)) << offset;
}; }
Ok((result, end_bytes)) Ok((result, end_bytes))
} else { } else {
Err(DecodeError::TooShort) Err(DecodeError::TooShort)
@ -53,15 +53,11 @@ impl Decodable for H256 {
fn ssz_decode(bytes: &[u8], index: usize) fn ssz_decode(bytes: &[u8], index: usize)
-> Result<(Self, usize), DecodeError> -> Result<(Self, usize), DecodeError>
{ {
if bytes.len() < 32 { if bytes.len() < 32 || bytes.len() - 32 < index {
return Err(DecodeError::TooShort) Err(DecodeError::TooShort)
}
else if bytes.len() - 32 < index {
return Err(DecodeError::TooShort)
} }
else { else {
return Ok((H256::from(&bytes[index..(index + 32)]), Ok((H256::from(&bytes[index..(index + 32)]), index + 32))
index + 32));
} }
} }
} }

View File

@ -15,8 +15,8 @@ use self::bytes::{ BytesMut, BufMut };
macro_rules! impl_encodable_for_uint { macro_rules! impl_encodable_for_uint {
($type: ident, $bit_size: expr) => { ($type: ident, $bit_size: expr) => {
impl Encodable for $type { impl Encodable for $type {
fn ssz_append(&self, s: &mut SszStream) #[allow(cast_lossless)]
{ fn ssz_append(&self, s: &mut SszStream) {
// Ensure bit size is valid // Ensure bit size is valid
assert!((0 < $bit_size) && assert!((0 < $bit_size) &&
($bit_size % 8 == 0) && ($bit_size % 8 == 0) &&
@ -35,7 +35,7 @@ macro_rules! impl_encodable_for_uint {
} }
// Append bytes to the SszStream // Append bytes to the SszStream
s.append_encoded_raw(&mut buf.to_vec()); s.append_encoded_raw(&buf.to_vec());
} }
} }
} }