Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eth2_libp2p
11 changes: 7 additions & 4 deletions ssz/src/bit_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'de, N: Unsigned> Deserialize<'de> for BitList<N> {
}

// `BitBox` serializes itself as a struct with multiple fields.
impl<N> Serialize for BitList<N> {
impl<N: Unsigned> Serialize for BitList<N> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut ssz_bytes = vec![];
self.write_variable(&mut ssz_bytes)
Expand All @@ -139,8 +139,11 @@ impl<N> Serialize for BitList<N> {
}
}

impl<N> SszSize for BitList<N> {
const SIZE: Size = Size::Variable { minimum_size: 1 };
impl<N: Unsigned> SszSize for BitList<N> {
const SIZE: Size = Size::Variable {
minimum: 1,
maximum: Ok(bytes_with_delimiting_bit(N::USIZE)),
};
}

impl<C, N: Unsigned> SszRead<C> for BitList<N> {
Expand All @@ -151,7 +154,7 @@ impl<C, N: Unsigned> SszRead<C> for BitList<N> {
}
}

impl<N> SszWrite for BitList<N> {
impl<N: Unsigned> SszWrite for BitList<N> {
fn write_variable(&self, bytes: &mut Vec<u8>) -> Result<(), WriteError> {
let length_before = bytes.len();
let length_after = length_before + bytes_with_delimiting_bit(self.len());
Expand Down
6 changes: 3 additions & 3 deletions ssz/src/byte_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl<'de, N: Unsigned> Deserialize<'de> for ByteList<N> {
}
}

impl<N> SszSize for ByteList<N> {
const SIZE: Size = Size::Variable { minimum_size: 0 };
impl<N: Unsigned> SszSize for ByteList<N> {
const SIZE: Size = Size::for_list(u8::SIZE, N::USIZE);
}

impl<C, N: Unsigned> SszRead<C> for ByteList<N> {
Expand All @@ -61,7 +61,7 @@ impl<C, N: Unsigned> SszRead<C> for ByteList<N> {
}
}

impl<N> SszWrite for ByteList<N> {
impl<N: Unsigned> SszWrite for ByteList<N> {
fn write_variable(&self, bytes: &mut Vec<u8>) -> Result<(), WriteError> {
self.bytes.write_variable(bytes)
}
Expand Down
6 changes: 3 additions & 3 deletions ssz/src/contiguous_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ impl<'de, T: Deserialize<'de>, N: Unsigned> Deserialize<'de> for ContiguousList<
}
}

impl<T: SszSize, N> SszSize for ContiguousList<T, N> {
const SIZE: Size = Size::Variable { minimum_size: 0 };
impl<T: SszSize, N: Unsigned> SszSize for ContiguousList<T, N> {
const SIZE: Size = Size::for_list(T::SIZE, N::USIZE);
}

impl<C, T: SszRead<C>, N: Unsigned> SszRead<C> for ContiguousList<T, N> {
Expand All @@ -109,7 +109,7 @@ impl<C, T: SszRead<C>, N: Unsigned> SszRead<C> for ContiguousList<T, N> {
}
}

impl<T: SszWrite, N> SszWrite for ContiguousList<T, N> {
impl<T: SszWrite, N: Unsigned> SszWrite for ContiguousList<T, N> {
fn write_variable(&self, bytes: &mut Vec<u8>) -> Result<(), WriteError> {
shared::write_list(bytes, self)
}
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/contiguous_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'de, T: Deserialize<'de>, N: ArrayLength<T>> Deserialize<'de> for Contiguou
}

impl<T: SszSize, N: ContiguousVectorElements<T>> SszSize for ContiguousVector<T, N> {
const SIZE: Size = T::SIZE.mul(N::USIZE);
const SIZE: Size = Size::for_vector(T::SIZE, N::USIZE);
}

impl<C, T: SszRead<C>, N: ContiguousVectorElements<T>> SszRead<C> for ContiguousVector<T, N> {
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/dynamic_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: Serialize> Serialize for DynamicList<T> {
}

impl<T: SszSize> SszSize for DynamicList<T> {
const SIZE: Size = Size::Variable { minimum_size: 0 };
const SIZE: Size = Size::for_list(T::SIZE, usize::MAX);
}

impl<T: SszReadDefault> SszRead<usize> for DynamicList<T> {
Expand Down
6 changes: 6 additions & 0 deletions ssz/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use crate::{
uint256::Uint256,
};

#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
pub enum SizeError {
#[error("maximum size does not fit in usize")]
MaximumSizeDoesNotFitInUsize,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
pub enum ReadError {
#[error("expected fixed-size value of {expected} bytes, found {actual} bytes")]
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use crate::{
contiguous_list::ContiguousList,
contiguous_vector::ContiguousVector,
dynamic_list::DynamicList,
error::{IndexError, PushError, ReadError, WriteError},
error::{IndexError, PushError, ReadError, SizeError, WriteError},
hc::Hc,
merkle_tree::{mix_in_length, MerkleTree, ProofWithLength},
persistent_list::PersistentList,
Expand Down
6 changes: 3 additions & 3 deletions ssz/src/persistent_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ impl<T: Serialize, N, B: BundleSize<T>> Serialize for PersistentList<T, N, B> {
}
}

impl<T: SszSize, N, B> SszSize for PersistentList<T, N, B> {
const SIZE: Size = Size::Variable { minimum_size: 0 };
impl<T: SszSize, N: Unsigned, B> SszSize for PersistentList<T, N, B> {
const SIZE: Size = Size::for_list(T::SIZE, N::USIZE);
}

impl<C, T: SszRead<C>, N: Unsigned, B: BundleSize<T>> SszRead<C> for PersistentList<T, N, B> {
Expand All @@ -246,7 +246,7 @@ impl<C, T: SszRead<C>, N: Unsigned, B: BundleSize<T>> SszRead<C> for PersistentL
}
}

impl<T: SszWrite, N, B: BundleSize<T>> SszWrite for PersistentList<T, N, B> {
impl<T: SszWrite, N: Unsigned, B: BundleSize<T>> SszWrite for PersistentList<T, N, B> {
fn write_variable(&self, bytes: &mut Vec<u8>) -> Result<(), WriteError> {
shared::write_list(bytes, self)
}
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/persistent_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ where
}

impl<T: SszSize, N: Unsigned + NonZero, B: BundleSize<T>> SszSize for PersistentVector<T, N, B> {
const SIZE: Size = T::SIZE.mul(N::USIZE);
const SIZE: Size = Size::for_vector(T::SIZE, N::USIZE);
}

impl<C, T, N, B> SszRead<C> for PersistentVector<T, N, B>
Expand Down
7 changes: 5 additions & 2 deletions ssz/src/porcelain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ pub trait SszWrite: SszSize {
self.write_fixed(bytes.as_mut_slice());
Ok(bytes)
}
Size::Variable { minimum_size } => {
let mut bytes = Vec::with_capacity(minimum_size);
Size::Variable {
minimum,
maximum: _,
} => {
let mut bytes = Vec::with_capacity(minimum);
self.write_variable(&mut bytes)?;
Ok(bytes)
}
Expand Down
Loading
Loading