Encode bitfield as list not vector

This commit is contained in:
Paul Hauner 2019-04-17 11:57:57 +10:00
parent ea8d5a3db9
commit 10a5d2657c
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
8 changed files with 37 additions and 15 deletions

View File

@ -6,7 +6,7 @@ use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, HexVisitor}; use serde_hex::{encode as hex_encode, HexVisitor};
use ssz::{decode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{decode, Decodable, DecodeError, Encodable, SszStream};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A BLS aggregate signature. /// A BLS aggregate signature.
/// ///
@ -166,7 +166,7 @@ impl<'de> Deserialize<'de> for AggregateSignature {
} }
} }
impl_tree_hash_for_ssz_bytes!(AggregateSignature); tree_hash_ssz_encoding_as_vector!(AggregateSignature);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -3,7 +3,7 @@ use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, PrefixedHexVisitor}; use serde_hex::{encode as hex_encode, PrefixedHexVisitor};
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A BLS aggregate signature. /// A BLS aggregate signature.
/// ///
@ -99,7 +99,7 @@ impl<'de> Deserialize<'de> for FakeAggregateSignature {
} }
} }
impl_tree_hash_for_ssz_bytes!(FakeAggregateSignature); tree_hash_ssz_encoding_as_vector!(FakeAggregateSignature);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -4,7 +4,7 @@ use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::HexVisitor; use serde_hex::HexVisitor;
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -74,7 +74,7 @@ impl Decodable for FakeSignature {
} }
} }
impl_tree_hash_for_ssz_bytes!(FakeSignature); tree_hash_ssz_encoding_as_vector!(FakeSignature);
impl Serialize for FakeSignature { impl Serialize for FakeSignature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

View File

@ -7,7 +7,7 @@ use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use std::default; use std::default;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -105,7 +105,7 @@ impl<'de> Deserialize<'de> for PublicKey {
} }
} }
impl_tree_hash_for_ssz_bytes!(PublicKey); tree_hash_ssz_encoding_as_vector!(PublicKey);
impl PartialEq for PublicKey { impl PartialEq for PublicKey {
fn eq(&self, other: &PublicKey) -> bool { fn eq(&self, other: &PublicKey) -> bool {

View File

@ -5,7 +5,7 @@ use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::HexVisitor; use serde_hex::HexVisitor;
use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -70,7 +70,7 @@ impl<'de> Deserialize<'de> for SecretKey {
} }
} }
impl_tree_hash_for_ssz_bytes!(SecretKey); tree_hash_ssz_encoding_as_vector!(SecretKey);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -5,7 +5,7 @@ use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::HexVisitor; use serde_hex::HexVisitor;
use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{decode, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_vector;
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -115,7 +115,7 @@ impl Decodable for Signature {
} }
} }
impl_tree_hash_for_ssz_bytes!(Signature); tree_hash_ssz_encoding_as_vector!(Signature);
impl Serialize for Signature { impl Serialize for Signature {
/// Serde serialization is compliant the Ethereum YAML test format. /// Serde serialization is compliant the Ethereum YAML test format.

View File

@ -9,7 +9,7 @@ use serde_hex::{encode, PrefixedHexVisitor};
use ssz::{Decodable, Encodable}; use ssz::{Decodable, Encodable};
use std::cmp; use std::cmp;
use std::default; use std::default;
use tree_hash::impl_tree_hash_for_ssz_bytes; use tree_hash::tree_hash_ssz_encoding_as_list;
/// A BooleanBitfield represents a set of booleans compactly stored as a vector of bits. /// A BooleanBitfield represents a set of booleans compactly stored as a vector of bits.
/// The BooleanBitfield is given a fixed size during construction. Reads outside of the current size return an out-of-bounds error. Writes outside of the current size expand the size of the set. /// The BooleanBitfield is given a fixed size during construction. Reads outside of the current size return an out-of-bounds error. Writes outside of the current size expand the size of the set.
@ -257,7 +257,7 @@ impl<'de> Deserialize<'de> for BooleanBitfield {
} }
} }
impl_tree_hash_for_ssz_bytes!(BooleanBitfield); tree_hash_ssz_encoding_as_list!(BooleanBitfield);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -28,7 +28,7 @@ fn num_nodes(num_leaves: usize) -> usize {
} }
#[macro_export] #[macro_export]
macro_rules! impl_tree_hash_for_ssz_bytes { macro_rules! tree_hash_ssz_encoding_as_vector {
($type: ident) => { ($type: ident) => {
impl tree_hash::TreeHash for $type { impl tree_hash::TreeHash for $type {
fn tree_hash_type() -> tree_hash::TreeHashType { fn tree_hash_type() -> tree_hash::TreeHashType {
@ -49,3 +49,25 @@ macro_rules! impl_tree_hash_for_ssz_bytes {
} }
}; };
} }
#[macro_export]
macro_rules! tree_hash_ssz_encoding_as_list {
($type: ident) => {
impl tree_hash::TreeHash for $type {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("List should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
ssz::ssz_encode(self).tree_hash_root()
}
}
};
}