Merge current master

This commit is contained in:
Kirk Baird
2019-02-20 13:59:11 +11:00
57 changed files with 677 additions and 973 deletions

View File

@@ -62,7 +62,7 @@ impl Serialize for AggregateSignature {
}
impl TreeHash for AggregateSignature {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}

View File

@@ -66,7 +66,7 @@ impl Serialize for PublicKey {
}
impl TreeHash for PublicKey {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}

View File

@@ -41,7 +41,7 @@ impl Decodable for SecretKey {
}
impl TreeHash for SecretKey {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
self.0.as_bytes().clone()
}
}

View File

@@ -73,7 +73,7 @@ impl Decodable for Signature {
}
impl TreeHash for Signature {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}

View File

@@ -187,8 +187,8 @@ impl Serialize for BooleanBitfield {
}
impl ssz::TreeHash for BooleanBitfield {
fn hash_tree_root(&self) -> Vec<u8> {
self.to_bytes().hash_tree_root()
fn hash_tree_root_internal(&self) -> Vec<u8> {
self.to_bytes().hash_tree_root_internal()
}
}

View File

@@ -39,6 +39,21 @@ impl Decodable for u8 {
}
}
impl Decodable for bool {
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
if index >= bytes.len() {
Err(DecodeError::TooShort)
} else {
let result = match bytes[index] {
0b0000_0000 => false,
0b1000_0000 => true,
_ => return Err(DecodeError::Invalid),
};
Ok((result, index + 1))
}
}
}
impl Decodable for H256 {
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
if bytes.len() < 32 || bytes.len() - 32 < index {
@@ -215,4 +230,20 @@ mod tests {
let result: u16 = decode_ssz(&vec![0, 0, 0, 0, 1], 3).unwrap().0;
assert_eq!(result, 1);
}
#[test]
fn test_decode_ssz_bool() {
let ssz = vec![0b0000_0000, 0b1000_0000];
let (result, index): (bool, usize) = decode_ssz(&ssz, 0).unwrap();
assert_eq!(index, 1);
assert_eq!(result, false);
let (result, index): (bool, usize) = decode_ssz(&ssz, 1).unwrap();
assert_eq!(index, 2);
assert_eq!(result, true);
let ssz = vec![0b0100_0000];
let result: Result<(bool, usize), DecodeError> = decode_ssz(&ssz, 0);
assert_eq!(result, Err(DecodeError::Invalid));
}
}

View File

@@ -46,6 +46,13 @@ impl_encodable_for_uint!(u32, 32);
impl_encodable_for_uint!(u64, 64);
impl_encodable_for_uint!(usize, 64);
impl Encodable for bool {
fn ssz_append(&self, s: &mut SszStream) {
let byte = if *self { 0b1000_0000 } else { 0b0000_0000 };
s.append_encoded_raw(&[byte]);
}
}
impl Encodable for H256 {
fn ssz_append(&self, s: &mut SszStream) {
s.append_encoded_raw(&self.to_vec());
@@ -206,4 +213,17 @@ mod tests {
ssz.append(&x);
assert_eq!(ssz.drain(), vec![255, 255, 255, 255, 255, 255, 255, 255]);
}
#[test]
fn test_ssz_encode_bool() {
let x: bool = false;
let mut ssz = SszStream::new();
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b0000_0000]);
let x: bool = true;
let mut ssz = SszStream::new();
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b1000_0000]);
}
}

View File

@@ -3,49 +3,49 @@ use super::{merkle_hash, ssz_encode, TreeHash};
use hashing::hash;
impl TreeHash for u8 {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u16 {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u32 {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u64 {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for usize {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for Address {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for H256 {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for [u8] {
fn hash_tree_root(&self) -> Vec<u8> {
fn hash_tree_root_internal(&self) -> Vec<u8> {
if self.len() > 32 {
return hash(&self);
}
@@ -57,12 +57,12 @@ impl<T> TreeHash for Vec<T>
where
T: TreeHash,
{
/// Returns the merkle_hash of a list of hash_tree_root values created
/// Returns the merkle_hash of a list of hash_tree_root_internal values created
/// from the given list.
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
/// to be handled properly (i.e. hashed) as byte array.
fn hash_tree_root(&self) -> Vec<u8> {
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect();
merkle_hash(&mut tree_hashes)
}
}
@@ -73,7 +73,7 @@ mod tests {
#[test]
fn test_impl_tree_hash_vec() {
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal();
assert_eq!(result.len(), 32);
}
}

View File

@@ -4,7 +4,14 @@ const SSZ_CHUNK_SIZE: usize = 128;
const HASHSIZE: usize = 32;
pub trait TreeHash {
fn hash_tree_root(&self) -> Vec<u8>;
fn hash_tree_root_internal(&self) -> Vec<u8>;
fn hash_tree_root(&self) -> Vec<u8> {
let mut result = self.hash_tree_root_internal();
if result.len() < HASHSIZE {
zpad(&mut result, HASHSIZE);
}
result
}
}
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
@@ -14,7 +21,8 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
let (mut chunk_size, mut chunkz) = list_to_blob(list);
// get data_len as bytes. It will hashed will the merkle root
let datalen = list.len().to_le_bytes();
let mut datalen = list.len().to_le_bytes().to_vec();
zpad(&mut datalen, 32);
// Tree-hash
while chunkz.len() > HASHSIZE {
@@ -36,33 +44,68 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
chunkz = new_chunkz;
}
chunkz.append(&mut datalen.to_vec());
chunkz.append(&mut datalen);
hash(&chunkz)
}
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
let chunk_size = if list.is_empty() {
let chunk_size = if list.is_empty() || list[0].len() < SSZ_CHUNK_SIZE {
SSZ_CHUNK_SIZE
} else if list[0].len() < SSZ_CHUNK_SIZE {
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
items_per_chunk * list[0].len()
} else {
list[0].len()
};
let mut data = Vec::new();
let (items_per_chunk, chunk_count) = if list.is_empty() {
(1, 1)
} else {
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
let chunk_count = list.len() / items_per_chunk;
(items_per_chunk, chunk_count)
};
let mut chunkz = Vec::new();
if list.is_empty() {
// handle and empty list
data.append(&mut vec![0; SSZ_CHUNK_SIZE]);
} else {
chunkz.append(&mut vec![0; SSZ_CHUNK_SIZE]);
} else if list[0].len() <= SSZ_CHUNK_SIZE {
// just create a blob here; we'll divide into
// chunked slices when we merklize
data.reserve(list[0].len() * list.len());
let mut chunk = Vec::with_capacity(chunk_size);
let mut item_count_in_chunk = 0;
chunkz.reserve(chunk_count * chunk_size);
for item in list.iter_mut() {
data.append(item);
item_count_in_chunk += 1;
chunk.append(item);
// completed chunk?
if item_count_in_chunk == items_per_chunk {
zpad(&mut chunk, chunk_size);
chunkz.append(&mut chunk);
item_count_in_chunk = 0;
}
}
// left-over uncompleted chunk?
if item_count_in_chunk != 0 {
zpad(&mut chunk, chunk_size);
chunkz.append(&mut chunk);
}
} else {
// chunks larger than SSZ_CHUNK_SIZE
chunkz.reserve(chunk_count * chunk_size);
for item in list.iter_mut() {
chunkz.append(item);
}
}
(chunk_size, data)
(chunk_size, chunkz)
}
/// right pads with zeros making 'bytes' 'size' in length
fn zpad(bytes: &mut Vec<u8>, size: usize) {
if bytes.len() < size {
bytes.resize(size, 0);
}
}
#[cfg(test)]

View File

@@ -0,0 +1,14 @@
[package]
name = "ssz_derive"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
description = "Procedural derive macros for SSZ encoding and decoding."
[lib]
proc-macro = true
[dependencies]
syn = "0.15"
quote = "0.6"
ssz = { path = "../ssz" }

View File

@@ -0,0 +1,128 @@
//! Provides the following procedural derive macros:
//!
//! - `#[derive(Encode)]`
//! - `#[derive(Decode)]`
//!
//! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the
//! order they are defined.
//!
//! Presently, only `structs` with named fields are supported. `enum`s and tuple-structs are
//! unsupported.
//!
//! Example:
//! ```
//! use ssz::{ssz_encode, Decodable};
//! use ssz_derive::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Foo {
//! pub bar: bool,
//! pub baz: u64,
//! }
//!
//! fn main() {
//! let foo = Foo {
//! bar: true,
//! baz: 42,
//! };
//!
//! let bytes = ssz_encode(&foo);
//!
//! let (decoded_foo, _i) = Foo::ssz_decode(&bytes, 0).unwrap();
//!
//! assert_eq!(foo.baz, decoded_foo.baz);
//! }
//! ```
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
/// Returns a Vec of `syn::Ident` for each named field in the struct.
///
/// # Panics
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::Ident> {
struct_data
.fields
.iter()
.map(|f| match &f.ident {
Some(ref ident) => ident,
_ => panic!("ssz_derive only supports named struct fields."),
})
.collect()
}
/// Implements `ssz::Encodable` for some `struct`.
///
/// Fields are encoded in the order they are defined.
#[proc_macro_derive(Encode)]
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as DeriveInput);
let name = &item.ident;
let struct_data = match &item.data {
syn::Data::Struct(s) => s,
_ => panic!("ssz_derive only supports structs."),
};
let field_idents = get_named_field_idents(&struct_data);
let output = quote! {
impl ssz::Encodable for #name {
fn ssz_append(&self, s: &mut ssz::SszStream) {
#(
s.append(&self.#field_idents);
)*
}
}
};
output.into()
}
/// Implements `ssz::Decodable` for some `struct`.
///
/// Fields are decoded in the order they are defined.
#[proc_macro_derive(Decode)]
pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as DeriveInput);
let name = &item.ident;
let struct_data = match &item.data {
syn::Data::Struct(s) => s,
_ => panic!("ssz_derive only supports structs."),
};
let field_idents = get_named_field_idents(&struct_data);
// Using a var in an iteration always consumes the var, therefore we must make a `fields_a` and
// a `fields_b` in order to perform two loops.
//
// https://github.com/dtolnay/quote/issues/8
let field_idents_a = &field_idents;
let field_idents_b = &field_idents;
let output = quote! {
impl ssz::Decodable for #name {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> {
#(
let (#field_idents_a, i) = <_>::ssz_decode(bytes, i)?;
)*
Ok((
Self {
#(
#field_idents_b,
)*
},
i
))
}
}
};
output.into()
}