From fa609b9c2f92bfb30bbc63ddc400ebb050aa6e7e Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 15 Mar 2022 15:17:55 +0200 Subject: [PATCH 01/45] Started with no-std feature --- ssz-rs/Cargo.toml | 7 ++++++- ssz-rs/src/lib.rs | 6 ++++++ ssz-rs/src/std.rs | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 ssz-rs/src/std.rs diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index e63be91a..7e88173d 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -16,8 +16,13 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["serde"] +default = ["std", "serde"] serde = ["dep:serde", "dep:hex"] +std = [ + "bitvec/std", + "sha2/std", + "lazy_static/spin", +] [dependencies] thiserror = "1.0.25" diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 9685fde8..9570a3a1 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -1,3 +1,8 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + mod array; mod bitlist; mod bitvector; @@ -14,6 +19,7 @@ mod uint; mod union; mod utils; mod vector; +mod std; pub use bitlist::Bitlist; pub use bitvector::Bitvector; diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs new file mode 100644 index 00000000..ad360b9c --- /dev/null +++ b/ssz-rs/src/std.rs @@ -0,0 +1,16 @@ +// Copyright 2020 Snowfork +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 . This file may not be +// copied, modified, or distributed except according to those terms. + +#[cfg(not(feature = "std"))] +pub use alloc::{vec, vec::Vec}; + +#[cfg(not(feature = "std"))] +pub use core::{option, ops, slice}; + +#[cfg(feature = "std")] +pub use std::{vec, vec::Vec, error, option, convert, fmt}; From ce34b540e7a5056357b7fdfb2ba6180e11b9d880 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 17 Mar 2022 08:34:06 +0200 Subject: [PATCH 02/45] Changes to make crate std compatible. --- ssz-rs/examples/container_with_some_types.rs | 4 +-- ssz-rs/src/array.rs | 1 + ssz-rs/src/bitlist.rs | 5 ++- ssz-rs/src/bitvector.rs | 5 ++- ssz-rs/src/boolean.rs | 1 + ssz-rs/src/de.rs | 18 +++++------ ssz-rs/src/lib.rs | 32 ++++++++++++++++++++ ssz-rs/src/list.rs | 6 +++- ssz-rs/src/merkleization/mod.rs | 19 ++++++------ ssz-rs/src/merkleization/node.rs | 5 +-- ssz-rs/src/ser.rs | 14 ++++----- ssz-rs/src/std.rs | 9 +++--- ssz-rs/src/uint.rs | 5 ++- ssz-rs/src/union.rs | 1 + 14 files changed, 78 insertions(+), 47 deletions(-) diff --git a/ssz-rs/examples/container_with_some_types.rs b/ssz-rs/examples/container_with_some_types.rs index b99a3d56..bf5d019f 100644 --- a/ssz-rs/examples/container_with_some_types.rs +++ b/ssz-rs/examples/container_with_some_types.rs @@ -52,7 +52,7 @@ fn main() { let encoding = match serialize(&foo) { Ok(encoding) => encoding, Err(e) => { - eprintln!("some error encoding: {}", e); + eprintln!("some error encoding: {:?}", e); return; } }; @@ -60,7 +60,7 @@ fn main() { let mut restored_foo = match Foo::<4>::deserialize(&encoding) { Ok(value) => value, Err(e) => { - eprintln!("some error decoding: {}", e); + eprintln!("some error decoding: {:?}", e); return; } }; diff --git a/ssz-rs/src/array.rs b/ssz-rs/src/array.rs index d77332eb..6145d868 100644 --- a/ssz-rs/src/array.rs +++ b/ssz-rs/src/array.rs @@ -9,6 +9,7 @@ use crate::merkleization::{ merkleize, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; +use crate::std::{vec, Vec}; use crate::{SimpleSerialize, Sized}; macro_rules! define_ssz_for_array_of_size { diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index b3cc092e..96a4bccd 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -5,9 +5,8 @@ use crate::merkleization::{ }; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; +use crate::std::{Vec, vec, Deref, DerefMut, fmt}; use bitvec::prelude::{BitVec, Lsb0}; -use std::fmt; -use std::ops::{Deref, DerefMut}; // +1 for length bit fn byte_length(bound: usize) -> usize { @@ -98,7 +97,7 @@ impl Bitlist { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize_with_length(&mut data, false)?; + let _ = self.serialize_with_length(&mut data, false).map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 7eb15877..21d51d65 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -3,10 +3,9 @@ use crate::error::TypeError; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; +use crate::std::{Vec, vec, Deref, DerefMut, fmt}; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; -use std::fmt; -use std::ops::{Deref, DerefMut}; fn byte_length(bound: usize) -> usize { (bound + 7) / 8 @@ -104,7 +103,7 @@ impl Bitvector { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize(&mut data)?; + let _ = self.serialize(&mut data).map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index aa92c733..f24606a1 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -2,6 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; +use crate::std::{Vec}; impl Sized for bool { fn is_variable_size() -> bool { diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 0e1c6ef8..2cb5caa3 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -1,20 +1,20 @@ use crate::error::{InstanceError, TypeError}; use crate::ser::BYTES_PER_LENGTH_OFFSET; +use crate::std::{vec, Vec}; use crate::SimpleSerialize; -use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Debug)] pub enum DeserializeError { - #[error("expected at least {expected} bytes when decoding but provided only {provided} bytes")] + // #[error("expected at least {expected} bytes when decoding but provided only {provided} bytes")] ExpectedFurtherInput { provided: usize, expected: usize }, - #[error("{provided} bytes given but only expected {expected} bytes")] + // #[error("{provided} bytes given but only expected {expected} bytes")] AdditionalInput { provided: usize, expected: usize }, - #[error("invalid byte {0:x} when decoding data of the expected type")] + // #[error("invalid byte {0:x} when decoding data of the expected type")] InvalidByte(u8), - #[error("invalid instance: {0}")] - InvalidInstance(#[from] InstanceError), - #[error("invalid type: {0}")] - InvalidType(#[from] TypeError), + // #[error("invalid instance: {0}")] + InvalidInstance(InstanceError), + // #[error("invalid type: {0}")] + InvalidType(TypeError), } pub trait Deserialize { diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 9570a3a1..cf5673ac 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -21,6 +21,7 @@ mod utils; mod vector; mod std; +pub use crate::std::{Vec, vec, Option}; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; @@ -49,6 +50,37 @@ pub trait SimpleSerialize: Serialize + Deserialize + Sized + Merkleized + Defaul } } +/// `serialize` is a convenience function for taking a value that +/// implements `SimpleSerialize` and attempting to encode it to +/// a `Vec` according to the SSZ spec. +pub fn serialize(value: &T) -> Result, SerializeError> +where + T: SimpleSerialize, +{ + let mut result = vec![]; + value.serialize(&mut result)?; + Ok(result) +} + +/// `deserialize` is a convenience function for taking an encoding +/// for some value that implements `SimpleSerialize` in a `&[u8]` +/// and attempting to deserialize that value from the byte representation. +pub fn deserialize(encoding: &[u8]) -> Result +where + T: SimpleSerialize, +{ + T::deserialize(encoding) +} + +#[derive(Debug)] +pub enum SimpleSerializeError { + Serialize, + Deserialize, + Merkleization, + List, + Vector, +} + /// The `prelude` contains common traits and types a user of this library /// would want to have handy with a simple (single) import. pub mod prelude { diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index 893d91ea..04550955 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -5,6 +5,10 @@ use crate::merkleization::{ BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; +use crate::std::{ + fmt, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, Vec, +}; +use crate::{SimpleSerialize, Sized}; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; @@ -290,7 +294,7 @@ pub struct IterMut<'a, T, const N: usize> where T: SimpleSerialize, { - inner: Enumerate>, + inner: Enumerate>, cache: &'a mut MerkleCache, } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 76ffc500..cda03009 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -2,13 +2,10 @@ mod cache; mod node; mod proofs; -use crate::ser::{Serialize, SerializeError}; +use crate::ser::Serialize; +use crate::std::{fmt, fmt::Debug, vec, Index, Option, Ordering, Vec}; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; -use std::cmp::Ordering; -use std::fmt::Debug; -use std::ops::Index; -use thiserror::Error; pub use cache::Cache as MerkleCache; pub use node::Node; @@ -21,11 +18,11 @@ pub trait Merkleized { fn hash_tree_root(&mut self) -> Result; } -#[derive(Error, Debug)] +#[derive(Debug)] pub enum MerkleizationError { - #[error("failed to serialize value: {0}")] - SerializationError(#[from] SerializeError), - #[error("cannot merkleize data that exceeds the declared limit {0}")] + // #[error("failed to serialize value: {0}")] + SerializationError(/*#[from]*/ SerializeError), + // #[error("cannot merkleize data that exceeds the declared limit {0}")] InputExceedsLimit(usize), } @@ -46,7 +43,9 @@ where { let mut buffer = vec![]; for value in values { - value.serialize(&mut buffer)?; + value + .serialize(&mut buffer) + .map_err(|_| MerkleizationError::SerializationError)?; } pack_bytes(&mut buffer); Ok(buffer) diff --git a/ssz-rs/src/merkleization/node.rs b/ssz-rs/src/merkleization/node.rs index 1b6aca83..27302140 100644 --- a/ssz-rs/src/merkleization/node.rs +++ b/ssz-rs/src/merkleization/node.rs @@ -1,8 +1,5 @@ use crate::prelude::*; -use std::array::TryFromSliceError; -use std::convert::AsRef; -use std::fmt; -use std::ops::{Index, IndexMut}; +use crate::std::{Index, IndexMut, Vec, vec, TryFromSliceError, fmt, AsRef}; #[derive(Default, Clone, Copy, PartialEq, Eq, SimpleSerialize)] pub struct Node(pub(crate) [u8; 32]); diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index 076ae3bb..80262b07 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -1,19 +1,19 @@ use crate::error::{InstanceError, TypeError}; +use crate::std::{vec, Vec}; use crate::SimpleSerialize; -use thiserror::Error; // NOTE: if this is changed, go change in `ssz_derive` as well! pub const BYTES_PER_LENGTH_OFFSET: usize = 4; const MAXIMUM_LENGTH: u64 = 2u64.pow((8 * BYTES_PER_LENGTH_OFFSET) as u32); -#[derive(Error, Debug)] +#[derive(Debug)] pub enum SerializeError { - #[error("the encoded length is {0} which exceeds the maximum length {MAXIMUM_LENGTH}")] + // #[error("the encoded length is {0} which exceeds the maximum length {MAXIMUM_LENGTH}")] MaximumEncodedLengthExceeded(usize), - #[error("invalid instance: {0}")] - InvalidInstance(#[from] InstanceError), - #[error("invalid type: {0}")] - InvalidType(#[from] TypeError), + // #[error("invalid instance: {0}")] + InvalidInstance(/*#[from]*/ InstanceError), + // #[error("invalid type: {0}")] + InvalidType(/*#[from]*/ TypeError), } pub trait Serialize { diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index ad360b9c..3a47bb2a 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -6,11 +6,10 @@ // http://www.apache.org/licenses/LICENSE-2.0>. This file may not be // copied, modified, or distributed except according to those terms. +#[cfg(feature = "std")] +pub use std::{ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, array::TryFromSliceError, slice::IterMut, vec, vec::Vec, error, option::Option, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, fmt, iter::FromIterator, iter::Enumerate, slice::SliceIndex, fmt::Debug, default::Default}; + #[cfg(not(feature = "std"))] pub use alloc::{vec, vec::Vec}; - #[cfg(not(feature = "std"))] -pub use core::{option, ops, slice}; - -#[cfg(feature = "std")] -pub use std::{vec, vec::Vec, error, option, convert, fmt}; +pub use core::{ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, slice::SliceIndex, slice::IterMut, option::Option, array::TryFromSliceError, iter::FromIterator, iter::Enumerate, convert::TryFrom, convert::TryInto, convert::AsRef, fmt, fmt::Debug, cmp::Ordering, default::Default}; diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index f978fec7..f9a42fea 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -2,9 +2,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; +use crate::std::{Vec, vec, Debug, Default, TryInto}; use num_bigint::BigUint; -use std::convert::TryInto; -use std::default::Default; macro_rules! define_uint { ($uint:ty) => { @@ -51,7 +50,7 @@ macro_rules! define_uint { impl Merkleized for $uint { fn hash_tree_root(&mut self) -> Result { let mut root = vec![]; - let _ = self.serialize(&mut root)?; + let _ = self.serialize(&mut root).map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut root); Ok(root.as_slice().try_into().expect("is valid root")) } diff --git a/ssz-rs/src/union.rs b/ssz-rs/src/union.rs index 8fe7157b..1ba9fe76 100644 --- a/ssz-rs/src/union.rs +++ b/ssz-rs/src/union.rs @@ -2,6 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{mix_in_selector, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; +use crate::std::Vec; /// `SimpleSerialize` is implemented for `Option` as a convenience /// when the schema is equivalent to one described by: From ca69391e2b281160d0e0c2abf4b37fe61d6a5db7 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 17 Mar 2022 09:39:49 +0200 Subject: [PATCH 03/45] Cleanup and improvements. --- ssz-rs/src/bitlist.rs | 2 +- ssz-rs/src/bitvector.rs | 2 +- ssz-rs/src/lib.rs | 2 +- ssz-rs/src/list.rs | 4 +++- ssz-rs/src/merkleization/mod.rs | 4 ++-- ssz-rs/src/std.rs | 4 ++-- ssz-rs/src/vector.rs | 14 ++++++++++++++ 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index 96a4bccd..b9b21ec5 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -5,7 +5,7 @@ use crate::merkleization::{ }; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, Deref, DerefMut, fmt}; +use crate::std::{Vec, vec, Deref, DerefMut, fmt, FromIterator}; use bitvec::prelude::{BitVec, Lsb0}; // +1 for length bit diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 21d51d65..79739d73 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -3,7 +3,7 @@ use crate::error::TypeError; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, Deref, DerefMut, fmt}; +use crate::std::{Vec, vec, Deref, DerefMut, fmt, FromIterator}; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index cf5673ac..d5e87641 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -21,7 +21,7 @@ mod utils; mod vector; mod std; -pub use crate::std::{Vec, vec, Option}; +pub use crate::std::{Vec, vec}; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index 04550955..b6455b60 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -8,7 +8,9 @@ use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::std::{ fmt, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, Vec, }; -use crate::{SimpleSerialize, Sized}; +use crate::std::{ + fmt, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, Vec, +}; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index cda03009..a1198568 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -2,8 +2,8 @@ mod cache; mod node; mod proofs; -use crate::ser::Serialize; -use crate::std::{fmt, fmt::Debug, vec, Index, Option, Ordering, Vec}; +use crate::ser::{Serialize, SerializeError}; +use crate::std::{fmt::Debug, vec, Index, Option, Ordering, Vec}; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index 3a47bb2a..33f061b0 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -7,9 +7,9 @@ // copied, modified, or distributed except according to those terms. #[cfg(feature = "std")] -pub use std::{ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, array::TryFromSliceError, slice::IterMut, vec, vec::Vec, error, option::Option, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, fmt, iter::FromIterator, iter::Enumerate, slice::SliceIndex, fmt::Debug, default::Default}; +pub use std::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, iter::FromIterator, iter::Enumerate, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, vec, vec::Vec}; #[cfg(not(feature = "std"))] pub use alloc::{vec, vec::Vec}; #[cfg(not(feature = "std"))] -pub use core::{ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, slice::SliceIndex, slice::IterMut, option::Option, array::TryFromSliceError, iter::FromIterator, iter::Enumerate, convert::TryFrom, convert::TryInto, convert::AsRef, fmt, fmt::Debug, cmp::Ordering, default::Default}; +pub use core::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, iter::Enumerate, iter::FromIterator, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex}; diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index cd1d53ee..5cc273b3 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -230,7 +230,21 @@ where }); } } +<<<<<<< HEAD deserialize_homogeneous_composite(encoding)?.try_into() +======= + let data = deserialize_homogeneous_composite(encoding)?; + data.try_into().map_err(|err| match err { + Error::IncorrectLength { expected, provided } => { + if expected < provided { + DeserializeError::ExtraInput + } else { + DeserializeError::InputTooShort + } + } + _ => unreachable!("variants not returned from `try_into`"), + }) +>>>>>>> 1b04857 (Cleanup and improvements.) } } From ca2b412b96955359734074bbacf6cedaf7b2b179 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 17 Mar 2022 09:43:49 +0200 Subject: [PATCH 04/45] Unreachable code warning fixed. --- ssz-rs/src/vector.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 5cc273b3..08dd654a 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -242,7 +242,6 @@ where DeserializeError::InputTooShort } } - _ => unreachable!("variants not returned from `try_into`"), }) >>>>>>> 1b04857 (Cleanup and improvements.) } From fac4bf0c6136fb2563bec6fc8bb972f557278f9e Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Fri, 18 Mar 2022 15:29:17 +0200 Subject: [PATCH 05/45] Downgrades edition. --- ssz-rs-derive/Cargo.toml | 2 +- ssz-rs/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ssz-rs-derive/Cargo.toml b/ssz-rs-derive/Cargo.toml index aad2c847..ea59210e 100644 --- a/ssz-rs-derive/Cargo.toml +++ b/ssz-rs-derive/Cargo.toml @@ -2,7 +2,7 @@ name = "ssz-rs-derive" version = "0.8.0" authors = ["Alex Stokes "] -edition = "2021" +edition = "2018" license = "MIT" description = "utility crate for deriving simple serialize implementations" repository = "https://github.com/ralexstokes/ssz-rs" diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 7e88173d..606315e6 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -3,7 +3,7 @@ name = "ssz-rs" version = "0.8.0" rust-version = "1.60" authors = ["Alex Stokes "] -edition = "2021" +edition = "2018" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" From f71b8b5e2f347f9c82b54905cec6f2a923ae939d Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Fri, 18 Mar 2022 15:36:57 +0200 Subject: [PATCH 06/45] Reverts downgrades edition. --- ssz-rs-derive/Cargo.toml | 2 +- ssz-rs/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ssz-rs-derive/Cargo.toml b/ssz-rs-derive/Cargo.toml index ea59210e..aad2c847 100644 --- a/ssz-rs-derive/Cargo.toml +++ b/ssz-rs-derive/Cargo.toml @@ -2,7 +2,7 @@ name = "ssz-rs-derive" version = "0.8.0" authors = ["Alex Stokes "] -edition = "2018" +edition = "2021" license = "MIT" description = "utility crate for deriving simple serialize implementations" repository = "https://github.com/ralexstokes/ssz-rs" diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 606315e6..7e88173d 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -3,7 +3,7 @@ name = "ssz-rs" version = "0.8.0" rust-version = "1.60" authors = ["Alex Stokes "] -edition = "2018" +edition = "2021" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" From 84e4a03a54a39f917787221707c777119eea319e Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 19 Apr 2022 13:04:00 +0200 Subject: [PATCH 07/45] Adds default features flag = false to dependency crates. --- ssz-rs/Cargo.toml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 7e88173d..3f8f89a1 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -16,23 +16,25 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["std", "serde"] -serde = ["dep:serde", "dep:hex"] +default = ["serde", "std"] std = [ "bitvec/std", "sha2/std", "lazy_static/spin", + "num-bigint/std", + "serde/std" ] +serde = ["dep:serde", "dep:hex"] [dependencies] thiserror = "1.0.25" -bitvec = "1.0.0" +bitvec = { version = "1.0.0", default-features = false, features=["alloc"]} ssz-rs-derive = { path = "../ssz-rs-derive"} -sha2 = "0.9.8" -lazy_static = "1.4.0" -serde = { version = "1.0", features = ["derive"], optional = true } -hex = {version = "0.4.3", optional = true } -num-bigint = "0.4.3" +sha2 = { version ="0.9.8", default-features = false} +lazy_static = { version = "1.4.0", features=["spin_no_std"]} +serde = { version = "1.0", features = ["derive"], optional=true } +hex = {version = "0.4.3", optional=true } +num-bigint = { version ="0.4.3", default-features = false} [dev-dependencies] hex-literal = "0.3.3" From 6fab3cef1c78a84a3a3b1706bfe2e429e2d9d491 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Sun, 22 Jan 2023 17:11:45 +0100 Subject: [PATCH 08/45] Fixes some errors in the new deserialize mod. --- ssz-rs/src/de.rs | 14 +++++++++++++- ssz-rs/src/ser.rs | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 2cb5caa3..dc20f230 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -1,6 +1,6 @@ use crate::error::{InstanceError, TypeError}; use crate::ser::BYTES_PER_LENGTH_OFFSET; -use crate::std::{vec, Vec}; +use crate::std::{vec, Debug, Display, Formatter, Vec}; use crate::SimpleSerialize; #[derive(Debug)] @@ -17,6 +17,18 @@ pub enum DeserializeError { InvalidType(TypeError), } +impl Debug for DeserializeError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "{:?}", self) + } +} + +impl Display for DeserializeError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "{:?}", self) + } +} + pub trait Deserialize { fn deserialize(encoding: &[u8]) -> Result where diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index 80262b07..bb634ed9 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -1,5 +1,5 @@ use crate::error::{InstanceError, TypeError}; -use crate::std::{vec, Vec}; +use crate::std::{vec, Debug, Display, Formatter, Vec}; use crate::SimpleSerialize; // NOTE: if this is changed, go change in `ssz_derive` as well! From cd94963e56e344e3670896b7a5ad757191a185cb Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Mon, 23 May 2022 10:45:21 +0200 Subject: [PATCH 09/45] Adds missing vec dependency for no-std version. --- ssz-rs/src/list.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index b6455b60..c78652c0 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -6,20 +6,14 @@ use crate::merkleization::{ }; use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::std::{ - fmt, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, Vec, -}; -use crate::std::{ - fmt, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, Vec, + fmt, vec, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, + Vec, }; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; -use std::iter::Enumerate; #[cfg(feature = "serde")] use std::marker::PhantomData; -use std::ops::{Deref, Index, IndexMut}; -use std::slice::SliceIndex; -use std::{fmt, slice}; /// A homogenous collection of a variable number of values. #[derive(Clone, Default)] From b677cab2e58f3d995b4a00380de09abda372b3ef Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 25 May 2022 08:56:36 +0200 Subject: [PATCH 10/45] Removes Rust version constraint, Cargo metadata. --- ssz-rs/Cargo.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 3f8f89a1..224e0400 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -1,13 +1,12 @@ [package] name = "ssz-rs" version = "0.8.0" -rust-version = "1.60" -authors = ["Alex Stokes "] +authors = ["Alex Stokes ", "Clara van Staden "] edition = "2021" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" -repository = "https://github.com/ralexstokes/ssz-rs" +repository = "https://github.com/Snowfork/ssz_rs" exclude = [ "scripts", "tests/data", From 89a4a940ff4ee7f39b88113ac35cb047d2cdd975 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 25 May 2022 16:28:33 +0200 Subject: [PATCH 11/45] Adds no-std for depedencies. --- ssz-rs/Cargo.toml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 224e0400..3f8f89a1 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "ssz-rs" version = "0.8.0" -authors = ["Alex Stokes ", "Clara van Staden "] +rust-version = "1.60" +authors = ["Alex Stokes "] edition = "2021" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" -repository = "https://github.com/Snowfork/ssz_rs" +repository = "https://github.com/ralexstokes/ssz-rs" exclude = [ "scripts", "tests/data", From 2b2bb758bc6659b6feaf5a0b6a37118ef49ad1a2 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 26 May 2022 09:31:26 +0200 Subject: [PATCH 12/45] Replaces std imports with mod std imports. --- ssz-rs/examples/another_container.rs | 1 + ssz-rs/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ssz-rs/examples/another_container.rs b/ssz-rs/examples/another_container.rs index b238cb0f..8075e9ad 100644 --- a/ssz-rs/examples/another_container.rs +++ b/ssz-rs/examples/another_container.rs @@ -1,5 +1,6 @@ use hex; use ssz_rs::prelude::*; +use ssz_rs::std::vec; #[derive(PartialEq, Eq, Debug, Default, SimpleSerialize)] struct SingleFieldTestStruct { diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index d5e87641..02e58171 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -19,7 +19,7 @@ mod uint; mod union; mod utils; mod vector; -mod std; +pub mod std; pub use crate::std::{Vec, vec}; pub use bitlist::Bitlist; From 8d6ac865896aeeb3064bd8d20390321edf4538b7 Mon Sep 17 00:00:00 2001 From: Clara van Staden Date: Thu, 17 Nov 2022 11:09:32 +0200 Subject: [PATCH 13/45] Cleanup and improvements (#4) Co-authored-by: claravanstaden --- ssz-rs/Cargo.toml | 4 ++-- ssz-rs/src/list.rs | 8 ++++---- ssz-rs/src/std.rs | 4 ++-- ssz-rs/src/uint.rs | 3 ++- ssz-rs/src/vector.rs | 23 ++++++++++++++++------- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 3f8f89a1..39940273 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -2,12 +2,12 @@ name = "ssz-rs" version = "0.8.0" rust-version = "1.60" -authors = ["Alex Stokes "] +authors = ["Alex Stokes ", "Clara van Staden "] edition = "2021" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" -repository = "https://github.com/ralexstokes/ssz-rs" +repository = "https://github.com/Snowfork/ssz_rs" exclude = [ "scripts", "tests/data", diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index c78652c0..ec72cfcb 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -6,8 +6,8 @@ use crate::merkleization::{ }; use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::std::{ - fmt, vec, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, SliceIndex, - Vec, + any, fmt, vec, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, + SliceIndex, Vec, }; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] @@ -84,7 +84,7 @@ where write!( f, "List<{}, {}>(len={}){:#?}", - std::any::type_name::(), + any::type_name::(), N, self.len(), self.data @@ -93,7 +93,7 @@ where write!( f, "List<{}, {}>(len={}){:?}", - std::any::type_name::(), + any::type_name::(), N, self.len(), self.data diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index 33f061b0..3123fedb 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -7,9 +7,9 @@ // copied, modified, or distributed except according to those terms. #[cfg(feature = "std")] -pub use std::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, iter::FromIterator, iter::Enumerate, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, vec, vec::Vec}; +pub use std::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, fmt::Display, fmt::Formatter, iter::FromIterator, iter::Enumerate, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, vec, vec::Vec, any}; #[cfg(not(feature = "std"))] pub use alloc::{vec, vec::Vec}; #[cfg(not(feature = "std"))] -pub use core::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, iter::Enumerate, iter::FromIterator, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex}; +pub use core::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, fmt::Display, fmt::Formatter, iter::Enumerate, iter::FromIterator, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, any}; diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index f9a42fea..9b31bdc9 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -98,6 +98,7 @@ impl U256 { } } +#[cfg(feature = "serde")] impl From for U256 { fn from(x: u64) -> Self { Self(x.into()) @@ -115,7 +116,7 @@ impl serde::Serialize for U256 { } } -#[cfg(feature = "serde")] +#[cfg(feature = "serde-rs")] impl<'de> serde::Deserialize<'de> for U256 { fn deserialize(deserializer: D) -> Result where diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 08dd654a..b7acac97 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -3,16 +3,13 @@ use crate::error::{InstanceError, TypeError}; use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; -use crate::ser::{serialize_composite, Serialize, SerializeError}; +use crate::ser::{serialize_composite, Serialize}; use crate::{SimpleSerialize, Sized}; +use crate::std::{Vec, vec, SliceIndex, IndexMut, Index, Deref, TryFrom, fmt, Debug, Display, Formatter, any}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; -use std::convert::TryFrom; -use std::fmt; #[cfg(feature = "serde")] use std::marker::PhantomData; -use std::ops::{Deref, Index, IndexMut}; -use std::slice::SliceIndex; /// A homogenous collection of a fixed number of values. /// NOTE: a `Vector` of length `0` is illegal. @@ -22,6 +19,18 @@ pub struct Vector { cache: MerkleCache, } +impl Debug for VectorError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "{:?}", self) + } +} + +impl Display for VectorError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "{:?}", self) + } +} + #[cfg(feature = "serde")] impl serde::Serialize for Vector { fn serialize(&self, serializer: S) -> Result @@ -116,7 +125,7 @@ where write!( f, "Vector<{}, {}>{:#?}", - std::any::type_name::(), + any::type_name::(), N, self.data ) @@ -124,7 +133,7 @@ where write!( f, "Vector<{}, {}>{:?}", - std::any::type_name::(), + any::type_name::(), N, self.data ) From a82a9f31d3a5dc813f55ee95e2599f1732c944c8 Mon Sep 17 00:00:00 2001 From: Clara van Staden Date: Thu, 17 Nov 2022 15:16:41 +0200 Subject: [PATCH 14/45] Fixes failure tests resulting in stack overflow error. (#5) Co-authored-by: claravanstaden --- ssz-rs/src/de.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index dc20f230..55b1d474 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -17,15 +17,16 @@ pub enum DeserializeError { InvalidType(TypeError), } -impl Debug for DeserializeError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) - } -} - impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) + match *self { + DeserializeError::InputTooShort => write!(f, "InputTooShort"), + DeserializeError::ExtraInput => write!(f, "ExtraInput"), + DeserializeError::InvalidInput => write!(f, "InvalidInput"), + DeserializeError::IOError => write!(f, "IOError"), + DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "TypeBoundsViolated bound: {} len {}", bound, len), + DeserializeError::IllegalType { bound } => write!(f, "IllegalType bound: {}", bound), + } } } From a7eda87216a4e269f6964e2452c2ee50279c045e Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 17 Nov 2022 15:29:34 +0200 Subject: [PATCH 15/45] Reverts changes that should be reverted before contributing back. --- ssz-rs/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 39940273..3f8f89a1 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -2,12 +2,12 @@ name = "ssz-rs" version = "0.8.0" rust-version = "1.60" -authors = ["Alex Stokes ", "Clara van Staden "] +authors = ["Alex Stokes "] edition = "2021" license = "MIT" readme = "../README.md" description = "ethereum's simple serialize" -repository = "https://github.com/Snowfork/ssz_rs" +repository = "https://github.com/ralexstokes/ssz-rs" exclude = [ "scripts", "tests/data", From a231367ae45353c9509619630e84cd3b62a507b5 Mon Sep 17 00:00:00 2001 From: Clara van Staden Date: Thu, 17 Nov 2022 15:48:15 +0200 Subject: [PATCH 16/45] Change error messages back to what they were. (#6) Co-authored-by: claravanstaden --- ssz-rs/src/de.rs | 10 +++++----- ssz-rs/src/merkleization/mod.rs | 16 +++++++++++++++- ssz-rs/src/ser.rs | 10 ++++++++++ ssz-rs/src/vector.rs | 10 +++------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 55b1d474..cbf2e0b8 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -20,12 +20,12 @@ pub enum DeserializeError { impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match *self { - DeserializeError::InputTooShort => write!(f, "InputTooShort"), - DeserializeError::ExtraInput => write!(f, "ExtraInput"), - DeserializeError::InvalidInput => write!(f, "InvalidInput"), + DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), + DeserializeError::ExtraInput => write!(f, "unexpected additional data provided when decoding"), + DeserializeError::InvalidInput => write!(f, "invalid data for expected type"), DeserializeError::IOError => write!(f, "IOError"), - DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "TypeBoundsViolated bound: {} len {}", bound, len), - DeserializeError::IllegalType { bound } => write!(f, "IllegalType bound: {}", bound), + DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len), + DeserializeError::IllegalType { bound } => write!(f, "the type for this value has an illegal bound of {}", bound), } } } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index a1198568..a9dbb027 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -3,7 +3,7 @@ mod node; mod proofs; use crate::ser::{Serialize, SerializeError}; -use crate::std::{fmt::Debug, vec, Index, Option, Ordering, Vec}; +use crate::std::{fmt::Debug, vec, Display, Formatter, Index, Option, Ordering, Vec}; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; @@ -26,6 +26,20 @@ pub enum MerkleizationError { InputExceedsLimit(usize), } +impl Display for MerkleizationError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + Self::SerializationError(err) => { + write!(f, "failed to serialize value: {err}") + } + Self::InputExceedsLimit(size) => write!( + f, + "cannot merkleize data that exceeds the declared limit: {size}", + ), + } + } +} + pub fn pack_bytes(buffer: &mut Vec) { let data_len = buffer.len(); if data_len % BYTES_PER_CHUNK != 0 { diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index bb634ed9..7febc234 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -16,6 +16,16 @@ pub enum SerializeError { InvalidType(/*#[from]*/ TypeError), } +impl Display for SerializeError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match *self { + SerializeError::MaximumEncodedLengthExceeded(size) => write!(f, "the encoded length is {} which exceeds the maximum length {}", size, MAXIMUM_LENGTH), + SerializeError::TypeBoundsViolated{ bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len), + SerializeError::IllegalType{ bound } => write!(f, "the type for this value has an illegal bound of {}", bound), + } + } +} + pub trait Serialize { /// Append an encoding of `self` to the `buffer`. /// Return the number of bytes written. diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index b7acac97..b8f5fc91 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -19,15 +19,11 @@ pub struct Vector { cache: MerkleCache, } -impl Debug for VectorError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) - } -} - impl Display for VectorError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) + match *self { + VectorError::IncorrectLength{ expected, provided } => write!(f, "incorrect number of elements {} to make a Vector of length {}", provided, expected), + } } } From a2df26b0e208e20a54d9acef0b97ca71467d2c1c Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 22 Nov 2022 10:10:05 +0200 Subject: [PATCH 17/45] Fixes missed merge-conflict change. --- ssz-rs/src/uint.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 9b31bdc9..46c4a89d 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -116,7 +116,7 @@ impl serde::Serialize for U256 { } } -#[cfg(feature = "serde-rs")] +#[cfg(feature = "serde")] impl<'de> serde::Deserialize<'de> for U256 { fn deserialize(deserializer: D) -> Result where From e402ea186bd062f9e71d363818afda93160decfb Mon Sep 17 00:00:00 2001 From: Clara van Staden Date: Wed, 23 Nov 2022 10:52:00 +0200 Subject: [PATCH 18/45] Update ssz-rs/Cargo.toml Co-authored-by: Alex Stokes --- ssz-rs/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 3f8f89a1..909bad6c 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -31,9 +31,9 @@ thiserror = "1.0.25" bitvec = { version = "1.0.0", default-features = false, features=["alloc"]} ssz-rs-derive = { path = "../ssz-rs-derive"} sha2 = { version ="0.9.8", default-features = false} -lazy_static = { version = "1.4.0", features=["spin_no_std"]} -serde = { version = "1.0", features = ["derive"], optional=true } -hex = {version = "0.4.3", optional=true } +lazy_static = { version = "1.4.0", features = ["spin_no_std"]} +serde = { version = "1.0", features = ["derive"], optional = true } +hex = {version = "0.4.3", optional = true } num-bigint = { version ="0.4.3", default-features = false} [dev-dependencies] From 2fc2ea2899bd7cbc0fb5a8c42986b78ff600ac88 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 12:42:58 +0200 Subject: [PATCH 19/45] Removes spin std from std feature. --- ssz-rs/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 909bad6c..37a8f27e 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -20,7 +20,6 @@ default = ["serde", "std"] std = [ "bitvec/std", "sha2/std", - "lazy_static/spin", "num-bigint/std", "serde/std" ] From 9a706f14f8aecd8c244632da8977d04c381df9b2 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 12:56:56 +0200 Subject: [PATCH 20/45] Use std lib for tests so crate std mod doesn't have to be public. --- ssz-rs/examples/another_container.rs | 1 - ssz-rs/src/lib.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ssz-rs/examples/another_container.rs b/ssz-rs/examples/another_container.rs index 8075e9ad..b238cb0f 100644 --- a/ssz-rs/examples/another_container.rs +++ b/ssz-rs/examples/another_container.rs @@ -1,6 +1,5 @@ use hex; use ssz_rs::prelude::*; -use ssz_rs::std::vec; #[derive(PartialEq, Eq, Debug, Default, SimpleSerialize)] struct SingleFieldTestStruct { diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 02e58171..d5e87641 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -19,7 +19,7 @@ mod uint; mod union; mod utils; mod vector; -pub mod std; +mod std; pub use crate::std::{Vec, vec}; pub use bitlist::Bitlist; From 7d636e7477d08810ef48948be38adb64994329f8 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 12:59:43 +0200 Subject: [PATCH 21/45] Removes unnecessary license comment. --- ssz-rs/src/std.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index 3123fedb..c44facf8 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -1,11 +1,3 @@ -// Copyright 2020 Snowfork -// -// SPDX-License-Identifier: Apache-2.0 -// -// Licensed under the Apache License, Version 2.0 . This file may not be -// copied, modified, or distributed except according to those terms. - #[cfg(feature = "std")] pub use std::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, fmt::Display, fmt::Formatter, iter::FromIterator, iter::Enumerate, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, vec, vec::Vec, any}; From ae37fa78f2f9d6d4791ff6a46962c222f8feb2bb Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 13:01:14 +0200 Subject: [PATCH 22/45] Removes incorrect serde feature. --- ssz-rs/src/uint.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 46c4a89d..f9a42fea 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -98,7 +98,6 @@ impl U256 { } } -#[cfg(feature = "serde")] impl From for U256 { fn from(x: u64) -> Self { Self(x.into()) From 25c49d981c0ac33b891fc238fa9a1736e41448c5 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 14:30:31 +0200 Subject: [PATCH 23/45] Std module improvement. --- ssz-rs/src/array.rs | 1 + ssz-rs/src/bitlist.rs | 2 +- ssz-rs/src/bitvector.rs | 2 +- ssz-rs/src/boolean.rs | 2 +- ssz-rs/src/de.rs | 18 ++++++++++++++---- ssz-rs/src/lib.rs | 2 +- ssz-rs/src/list.rs | 7 ++----- ssz-rs/src/merkleization/mod.rs | 2 +- ssz-rs/src/merkleization/node.rs | 2 +- ssz-rs/src/ser.rs | 20 ++++++++++++++++---- ssz-rs/src/std.rs | 24 +++++++++++++++++++----- ssz-rs/src/uint.rs | 2 +- ssz-rs/src/union.rs | 2 +- ssz-rs/src/vector.rs | 2 +- 14 files changed, 61 insertions(+), 27 deletions(-) diff --git a/ssz-rs/src/array.rs b/ssz-rs/src/array.rs index 6145d868..749d89f0 100644 --- a/ssz-rs/src/array.rs +++ b/ssz-rs/src/array.rs @@ -9,6 +9,7 @@ use crate::merkleization::{ merkleize, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; +use crate::std::*; use crate::std::{vec, Vec}; use crate::{SimpleSerialize, Sized}; diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index b9b21ec5..a649e3de 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -5,7 +5,7 @@ use crate::merkleization::{ }; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, Deref, DerefMut, fmt, FromIterator}; +use crate::std::*; use bitvec::prelude::{BitVec, Lsb0}; // +1 for length bit diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 79739d73..cc8cb625 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -3,7 +3,7 @@ use crate::error::TypeError; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, Deref, DerefMut, fmt, FromIterator}; +use crate::std::*; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index f24606a1..1122f257 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -2,7 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec}; +use crate::std::*; impl Sized for bool { fn is_variable_size() -> bool { diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index cbf2e0b8..58044490 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -1,6 +1,6 @@ use crate::error::{InstanceError, TypeError}; use crate::ser::BYTES_PER_LENGTH_OFFSET; -use crate::std::{vec, Debug, Display, Formatter, Vec}; +use crate::std::*; use crate::SimpleSerialize; #[derive(Debug)] @@ -21,11 +21,21 @@ impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match *self { DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), - DeserializeError::ExtraInput => write!(f, "unexpected additional data provided when decoding"), + DeserializeError::ExtraInput => { + write!(f, "unexpected additional data provided when decoding") + } DeserializeError::InvalidInput => write!(f, "invalid data for expected type"), DeserializeError::IOError => write!(f, "IOError"), - DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len), - DeserializeError::IllegalType { bound } => write!(f, "the type for this value has an illegal bound of {}", bound), + DeserializeError::TypeBoundsViolated { bound, len } => write!( + f, + "the type for this value has a bound of {} but the value has {} elements", + bound, len + ), + DeserializeError::IllegalType { bound } => write!( + f, + "the type for this value has an illegal bound of {}", + bound + ), } } } diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index d5e87641..50e7b011 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -21,7 +21,7 @@ mod utils; mod vector; mod std; -pub use crate::std::{Vec, vec}; +pub use crate::std::*; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index ec72cfcb..e2a14bb2 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -5,10 +5,7 @@ use crate::merkleization::{ BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::std::{ - any, fmt, vec, Deref, Enumerate, FromIterator, Index, IndexMut, IterMut as StdIterMut, - SliceIndex, Vec, -}; +use crate::std::*; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; @@ -290,7 +287,7 @@ pub struct IterMut<'a, T, const N: usize> where T: SimpleSerialize, { - inner: Enumerate>, + inner: Enumerate>, cache: &'a mut MerkleCache, } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index a9dbb027..a4463d41 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -3,7 +3,7 @@ mod node; mod proofs; use crate::ser::{Serialize, SerializeError}; -use crate::std::{fmt::Debug, vec, Display, Formatter, Index, Option, Ordering, Vec}; +use crate::std::*; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; diff --git a/ssz-rs/src/merkleization/node.rs b/ssz-rs/src/merkleization/node.rs index 27302140..4ef7631e 100644 --- a/ssz-rs/src/merkleization/node.rs +++ b/ssz-rs/src/merkleization/node.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use crate::std::{Index, IndexMut, Vec, vec, TryFromSliceError, fmt, AsRef}; +use crate::std::*; #[derive(Default, Clone, Copy, PartialEq, Eq, SimpleSerialize)] pub struct Node(pub(crate) [u8; 32]); diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index 7febc234..bf22ee5e 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -1,5 +1,5 @@ use crate::error::{InstanceError, TypeError}; -use crate::std::{vec, Debug, Display, Formatter, Vec}; +use crate::std::*; use crate::SimpleSerialize; // NOTE: if this is changed, go change in `ssz_derive` as well! @@ -19,9 +19,21 @@ pub enum SerializeError { impl Display for SerializeError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match *self { - SerializeError::MaximumEncodedLengthExceeded(size) => write!(f, "the encoded length is {} which exceeds the maximum length {}", size, MAXIMUM_LENGTH), - SerializeError::TypeBoundsViolated{ bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len), - SerializeError::IllegalType{ bound } => write!(f, "the type for this value has an illegal bound of {}", bound), + SerializeError::MaximumEncodedLengthExceeded(size) => write!( + f, + "the encoded length is {} which exceeds the maximum length {}", + size, MAXIMUM_LENGTH + ), + SerializeError::TypeBoundsViolated { bound, len } => write!( + f, + "the type for this value has a bound of {} but the value has {} elements", + bound, len + ), + SerializeError::IllegalType { bound } => write!( + f, + "the type for this value has an illegal bound of {}", + bound + ), } } } diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index c44facf8..27fe1bd8 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -1,7 +1,21 @@ -#[cfg(feature = "std")] -pub use std::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, fmt::Display, fmt::Formatter, iter::FromIterator, iter::Enumerate, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, vec, vec::Vec, any}; +mod core { + #[cfg(not(feature = "std"))] + pub use core::*; + #[cfg(feature = "std")] + pub use std::*; +} + +pub use self::core::{cmp, iter, slice, any}; + +pub use self::core::fmt::{self, Debug, Display, Formatter}; +pub use self::core::ops::{Index, IndexMut, Deref, DerefMut}; +pub use self::core::slice::{IterMut, SliceIndex}; +pub use self::core::array::TryFromSliceError; +pub use self::iter::Enumerate; +pub use self::cmp::Ordering; #[cfg(not(feature = "std"))] -pub use alloc::{vec, vec::Vec}; -#[cfg(not(feature = "std"))] -pub use core::{array::TryFromSliceError, cmp::Ordering, convert::AsRef, convert::TryFrom, convert::TryInto, default::Default, fmt, fmt::Debug, fmt::Display, fmt::Formatter, iter::Enumerate, iter::FromIterator, ops::DerefMut, ops::Deref, ops::Index, ops::IndexMut, option::Option, slice::IterMut, slice::SliceIndex, any}; +pub use alloc::{vec::Vec, vec}; + +#[cfg(feature = "std")] +pub use std::vec::Vec; diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index f9a42fea..0b337a53 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -2,7 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, Debug, Default, TryInto}; +use crate::std::*; use num_bigint::BigUint; macro_rules! define_uint { diff --git a/ssz-rs/src/union.rs b/ssz-rs/src/union.rs index 1ba9fe76..5779e6d3 100644 --- a/ssz-rs/src/union.rs +++ b/ssz-rs/src/union.rs @@ -2,7 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{mix_in_selector, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; -use crate::std::Vec; +use crate::std::*; /// `SimpleSerialize` is implemented for `Option` as a convenience /// when the schema is equivalent to one described by: diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index b8f5fc91..338d14ee 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -5,7 +5,7 @@ use crate::merkleization::{ }; use crate::ser::{serialize_composite, Serialize}; use crate::{SimpleSerialize, Sized}; -use crate::std::{Vec, vec, SliceIndex, IndexMut, Index, Deref, TryFrom, fmt, Debug, Display, Formatter, any}; +use crate::std::*; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] From 8c0082d09b9910c4ad8f4c94b0e79d1e15e64965 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 23 Nov 2022 20:01:45 +0200 Subject: [PATCH 24/45] cargo fmt --- ssz-rs/src/array.rs | 1 - ssz-rs/src/bitlist.rs | 6 ++++-- ssz-rs/src/bitvector.rs | 6 ++++-- ssz-rs/src/boolean.rs | 2 +- ssz-rs/src/lib.rs | 2 +- ssz-rs/src/std.rs | 10 +++++----- ssz-rs/src/uint.rs | 6 ++++-- ssz-rs/src/union.rs | 2 +- ssz-rs/src/vector.rs | 31 ++++++++----------------------- 9 files changed, 28 insertions(+), 38 deletions(-) diff --git a/ssz-rs/src/array.rs b/ssz-rs/src/array.rs index 749d89f0..abe8749e 100644 --- a/ssz-rs/src/array.rs +++ b/ssz-rs/src/array.rs @@ -10,7 +10,6 @@ use crate::merkleization::{ }; use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::std::*; -use crate::std::{vec, Vec}; use crate::{SimpleSerialize, Sized}; macro_rules! define_ssz_for_array_of_size { diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index a649e3de..e23f7ba3 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -4,8 +4,8 @@ use crate::merkleization::{ merkleize, mix_in_length, pack_bytes, MerkleizationError, Merkleized, Node, }; use crate::ser::{Serialize, SerializeError}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; use bitvec::prelude::{BitVec, Lsb0}; // +1 for length bit @@ -97,7 +97,9 @@ impl Bitlist { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize_with_length(&mut data, false).map_err(|_| MerkleizationError::SerializationError); + let _ = self + .serialize_with_length(&mut data, false) + .map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index cc8cb625..b42d1b78 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -2,8 +2,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::error::TypeError; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; @@ -103,7 +103,9 @@ impl Bitvector { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize(&mut data).map_err(|_| MerkleizationError::SerializationError); + let _ = self + .serialize(&mut data) + .map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index 1122f257..91f918a3 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -1,8 +1,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; impl Sized for bool { fn is_variable_size() -> bool { diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 50e7b011..0509f0c8 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -15,11 +15,11 @@ mod merkleization; mod ser; #[cfg(feature = "serde")] mod serde_test; +mod std; mod uint; mod union; mod utils; mod vector; -mod std; pub use crate::std::*; pub use bitlist::Bitlist; diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs index 27fe1bd8..ab8bb5c7 100644 --- a/ssz-rs/src/std.rs +++ b/ssz-rs/src/std.rs @@ -5,17 +5,17 @@ mod core { pub use std::*; } -pub use self::core::{cmp, iter, slice, any}; +pub use self::core::{any, cmp, iter, slice}; +pub use self::cmp::Ordering; +pub use self::core::array::TryFromSliceError; pub use self::core::fmt::{self, Debug, Display, Formatter}; -pub use self::core::ops::{Index, IndexMut, Deref, DerefMut}; +pub use self::core::ops::{Deref, DerefMut, Index, IndexMut}; pub use self::core::slice::{IterMut, SliceIndex}; -pub use self::core::array::TryFromSliceError; pub use self::iter::Enumerate; -pub use self::cmp::Ordering; #[cfg(not(feature = "std"))] -pub use alloc::{vec::Vec, vec}; +pub use alloc::{vec, vec::Vec}; #[cfg(feature = "std")] pub use std::vec::Vec; diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 0b337a53..7003a0e2 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -1,8 +1,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; use num_bigint::BigUint; macro_rules! define_uint { @@ -50,7 +50,9 @@ macro_rules! define_uint { impl Merkleized for $uint { fn hash_tree_root(&mut self) -> Result { let mut root = vec![]; - let _ = self.serialize(&mut root).map_err(|_| MerkleizationError::SerializationError); + let _ = self + .serialize(&mut root) + .map_err(|_| MerkleizationError::SerializationError); pack_bytes(&mut root); Ok(root.as_slice().try_into().expect("is valid root")) } diff --git a/ssz-rs/src/union.rs b/ssz-rs/src/union.rs index 5779e6d3..d9f37963 100644 --- a/ssz-rs/src/union.rs +++ b/ssz-rs/src/union.rs @@ -1,8 +1,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{mix_in_selector, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; /// `SimpleSerialize` is implemented for `Option` as a convenience /// when the schema is equivalent to one described by: diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 338d14ee..28365724 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -4,8 +4,8 @@ use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize}; -use crate::{SimpleSerialize, Sized}; use crate::std::*; +use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] @@ -19,10 +19,14 @@ pub struct Vector { cache: MerkleCache, } -impl Display for VectorError { +impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match *self { - VectorError::IncorrectLength{ expected, provided } => write!(f, "incorrect number of elements {} to make a Vector of length {}", provided, expected), + Error::IncorrectLengthVector { expected, provided } => write!( + f, + "incorrect number of elements {} to make a Vector of length {}", + provided, expected + ), } } } @@ -126,13 +130,7 @@ where self.data ) } else { - write!( - f, - "Vector<{}, {}>{:?}", - any::type_name::(), - N, - self.data - ) + write!(f, "Vector<{}, {}>{:?}", any::type_name::(), N, self.data) } } } @@ -235,20 +233,7 @@ where }); } } -<<<<<<< HEAD deserialize_homogeneous_composite(encoding)?.try_into() -======= - let data = deserialize_homogeneous_composite(encoding)?; - data.try_into().map_err(|err| match err { - Error::IncorrectLength { expected, provided } => { - if expected < provided { - DeserializeError::ExtraInput - } else { - DeserializeError::InputTooShort - } - } - }) ->>>>>>> 1b04857 (Cleanup and improvements.) } } From 897aeef06221cb67e39e6e8d329899c9b00a0bf4 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 14 Dec 2022 13:45:19 +0200 Subject: [PATCH 25/45] Removes exposing std crate publicly. Adds back Vector export. --- ssz-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 0509f0c8..58539177 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -21,7 +21,7 @@ mod union; mod utils; mod vector; -pub use crate::std::*; +use crate::std::*; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; From ae17a5ccecfbea3837628a08ce55d02e72b5729f Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 14 Dec 2022 13:47:12 +0200 Subject: [PATCH 26/45] Implements From impl from SerializeError to MerkleizationError::SerializationError. --- ssz-rs/examples/container_with_some_types.rs | 2 +- ssz-rs/src/bitlist.rs | 5 ++--- ssz-rs/src/bitvector.rs | 4 +--- ssz-rs/src/merkleization/mod.rs | 11 ++++++++--- ssz-rs/src/uint.rs | 4 +--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ssz-rs/examples/container_with_some_types.rs b/ssz-rs/examples/container_with_some_types.rs index bf5d019f..c1b73b9a 100644 --- a/ssz-rs/examples/container_with_some_types.rs +++ b/ssz-rs/examples/container_with_some_types.rs @@ -60,7 +60,7 @@ fn main() { let mut restored_foo = match Foo::<4>::deserialize(&encoding) { Ok(value) => value, Err(e) => { - eprintln!("some error decoding: {:?}", e); + eprintln!("some error decoding: {}", e); return; } }; diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index e23f7ba3..7a1ee3a7 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -97,9 +97,7 @@ impl Bitlist { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self - .serialize_with_length(&mut data, false) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize_with_length(&mut data, false); pack_bytes(&mut data); Ok(data) } @@ -230,6 +228,7 @@ impl FromIterator for Bitlist { #[cfg(test)] mod tests { + use std::hash::Hash; use super::*; use crate::serialize; diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index b42d1b78..e0d28ea1 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -103,9 +103,7 @@ impl Bitvector { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self - .serialize(&mut data) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize(&mut data); pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index a4463d41..1c0fa1bb 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -10,6 +10,7 @@ use sha2::{Digest, Sha256}; pub use cache::Cache as MerkleCache; pub use node::Node; pub use proofs::is_valid_merkle_branch; +use crate::SerializeError; pub(crate) const BYTES_PER_CHUNK: usize = 32; @@ -26,6 +27,12 @@ pub enum MerkleizationError { InputExceedsLimit(usize), } +impl From for MerkleizationError { + fn from(_: SerializeError) -> Self { + return MerkleizationError::SerializationError + } +} + impl Display for MerkleizationError { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { @@ -57,9 +64,7 @@ where { let mut buffer = vec![]; for value in values { - value - .serialize(&mut buffer) - .map_err(|_| MerkleizationError::SerializationError)?; + value.serialize(&mut buffer); } pack_bytes(&mut buffer); Ok(buffer) diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 7003a0e2..cc4113bc 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -50,9 +50,7 @@ macro_rules! define_uint { impl Merkleized for $uint { fn hash_tree_root(&mut self) -> Result { let mut root = vec![]; - let _ = self - .serialize(&mut root) - .map_err(|_| MerkleizationError::SerializationError); + let _ = self.serialize(&mut root)?; pack_bytes(&mut root); Ok(root.as_slice().try_into().expect("is valid root")) } From 4a27c1fb0fe64eede210ea084ee3a2b98a95a899 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 14 Dec 2022 13:58:14 +0200 Subject: [PATCH 27/45] Removes std mod and moves imports into lib. --- ssz-rs/src/array.rs | 2 +- ssz-rs/src/bitlist.rs | 3 +-- ssz-rs/src/bitvector.rs | 2 +- ssz-rs/src/boolean.rs | 2 +- ssz-rs/src/de.rs | 2 +- ssz-rs/src/lib.rs | 27 +++++++++++++++++++++++++-- ssz-rs/src/list.rs | 4 ++-- ssz-rs/src/merkleization/mod.rs | 8 ++++---- ssz-rs/src/merkleization/node.rs | 2 +- ssz-rs/src/ser.rs | 2 +- ssz-rs/src/std.rs | 21 --------------------- ssz-rs/src/uint.rs | 2 +- ssz-rs/src/union.rs | 2 +- ssz-rs/src/vector.rs | 2 +- 14 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 ssz-rs/src/std.rs diff --git a/ssz-rs/src/array.rs b/ssz-rs/src/array.rs index abe8749e..8e8b87e3 100644 --- a/ssz-rs/src/array.rs +++ b/ssz-rs/src/array.rs @@ -9,7 +9,7 @@ use crate::merkleization::{ merkleize, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; macro_rules! define_ssz_for_array_of_size { diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index 7a1ee3a7..70e0d1b2 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -4,7 +4,7 @@ use crate::merkleization::{ merkleize, mix_in_length, pack_bytes, MerkleizationError, Merkleized, Node, }; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; use bitvec::prelude::{BitVec, Lsb0}; @@ -228,7 +228,6 @@ impl FromIterator for Bitlist { #[cfg(test)] mod tests { - use std::hash::Hash; use super::*; use crate::serialize; diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index e0d28ea1..349f83e9 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -2,7 +2,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::error::TypeError; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index 91f918a3..171c9f37 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; impl Sized for bool { diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 58044490..0f99d4ee 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -1,6 +1,6 @@ use crate::error::{InstanceError, TypeError}; use crate::ser::BYTES_PER_LENGTH_OFFSET; -use crate::std::*; +use crate::lib::*; use crate::SimpleSerialize; #[derive(Debug)] diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 58539177..502d4c8b 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -15,13 +15,11 @@ mod merkleization; mod ser; #[cfg(feature = "serde")] mod serde_test; -mod std; mod uint; mod union; mod utils; mod vector; -use crate::std::*; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; @@ -32,6 +30,31 @@ pub use ser::{Serialize, SerializeError}; pub use uint::U256; pub use utils::*; pub use vector::Vector; +use crate::lib::*; + +mod lib { + mod core { + #[cfg(not(feature = "std"))] + pub use core::*; + #[cfg(feature = "std")] + pub use std::*; + } + + pub use self::core::{any, cmp, iter, slice}; + + pub use self::cmp::Ordering; + pub use self::core::array::TryFromSliceError; + pub use self::core::fmt::{self, Debug, Display, Formatter}; + pub use self::core::ops::{Deref, DerefMut, Index, IndexMut}; + pub use self::core::slice::{IterMut, SliceIndex}; + pub use self::iter::Enumerate; + + #[cfg(not(feature = "std"))] + pub use alloc::{vec, vec::Vec}; + + #[cfg(feature = "std")] + pub use std::vec::Vec; +} /// `Sized` is a trait for types that can /// provide sizing information relevant for the SSZ spec. diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index e2a14bb2..47964781 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -5,7 +5,7 @@ use crate::merkleization::{ BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; @@ -287,7 +287,7 @@ pub struct IterMut<'a, T, const N: usize> where T: SimpleSerialize, { - inner: Enumerate>, + inner: Enumerate>, cache: &'a mut MerkleCache, } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 1c0fa1bb..24a3c4a5 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -2,15 +2,15 @@ mod cache; mod node; mod proofs; +use crate::lib::*; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; +use crate::SerializeError; pub use cache::Cache as MerkleCache; pub use node::Node; pub use proofs::is_valid_merkle_branch; -use crate::SerializeError; pub(crate) const BYTES_PER_CHUNK: usize = 32; @@ -29,7 +29,7 @@ pub enum MerkleizationError { impl From for MerkleizationError { fn from(_: SerializeError) -> Self { - return MerkleizationError::SerializationError + return MerkleizationError::SerializationError; } } @@ -64,7 +64,7 @@ where { let mut buffer = vec![]; for value in values { - value.serialize(&mut buffer); + value.serialize(&mut buffer)?; } pack_bytes(&mut buffer); Ok(buffer) diff --git a/ssz-rs/src/merkleization/node.rs b/ssz-rs/src/merkleization/node.rs index 4ef7631e..2eab8af4 100644 --- a/ssz-rs/src/merkleization/node.rs +++ b/ssz-rs/src/merkleization/node.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use crate::std::*; +use crate::lib::*; #[derive(Default, Clone, Copy, PartialEq, Eq, SimpleSerialize)] pub struct Node(pub(crate) [u8; 32]); diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index bf22ee5e..18f57dc4 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -1,5 +1,5 @@ use crate::error::{InstanceError, TypeError}; -use crate::std::*; +use crate::lib::*; use crate::SimpleSerialize; // NOTE: if this is changed, go change in `ssz_derive` as well! diff --git a/ssz-rs/src/std.rs b/ssz-rs/src/std.rs deleted file mode 100644 index ab8bb5c7..00000000 --- a/ssz-rs/src/std.rs +++ /dev/null @@ -1,21 +0,0 @@ -mod core { - #[cfg(not(feature = "std"))] - pub use core::*; - #[cfg(feature = "std")] - pub use std::*; -} - -pub use self::core::{any, cmp, iter, slice}; - -pub use self::cmp::Ordering; -pub use self::core::array::TryFromSliceError; -pub use self::core::fmt::{self, Debug, Display, Formatter}; -pub use self::core::ops::{Deref, DerefMut, Index, IndexMut}; -pub use self::core::slice::{IterMut, SliceIndex}; -pub use self::iter::Enumerate; - -#[cfg(not(feature = "std"))] -pub use alloc::{vec, vec::Vec}; - -#[cfg(feature = "std")] -pub use std::vec::Vec; diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index cc4113bc..5d9a81aa 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; use num_bigint::BigUint; diff --git a/ssz-rs/src/union.rs b/ssz-rs/src/union.rs index d9f37963..6f841f2c 100644 --- a/ssz-rs/src/union.rs +++ b/ssz-rs/src/union.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; use crate::merkleization::{mix_in_selector, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; /// `SimpleSerialize` is implemented for `Option` as a convenience diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 28365724..30523781 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -4,7 +4,7 @@ use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize}; -use crate::std::*; +use crate::lib::*; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; From 97e6124bf0d7e854e226dbb3a3d48d83b9435546 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 14 Dec 2022 14:44:24 +0200 Subject: [PATCH 28/45] Adds Display implementation for SimpleSerializeError. --- ssz-rs/src/lib.rs | 24 +++++++++++++++++++----- ssz-rs/src/list.rs | 4 ++-- ssz-rs/src/vector.rs | 4 ++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 502d4c8b..4b7b9016 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -31,6 +31,8 @@ pub use uint::U256; pub use utils::*; pub use vector::Vector; use crate::lib::*; +use crate::list::ListError; +use crate::vector::VectorError; mod lib { mod core { @@ -97,11 +99,23 @@ where #[derive(Debug)] pub enum SimpleSerializeError { - Serialize, - Deserialize, - Merkleization, - List, - Vector, + Serialize(SerializeError), + Deserialize(DeserializeError), + Merkleization(MerkleizationError), + List(ListError), + Vector(VectorError), +} + +impl Display for SimpleSerializeError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + SimpleSerializeError::Serialize(error) => write!(f, "serialize error: {}", error), + SimpleSerializeError::Deserialize(error) => write!(f, "deserialize error: {}", error), + SimpleSerializeError::Merkleization(error) => write!(f, "merkleization error: {}", error), + SimpleSerializeError::List(error) => write!(f, "list error: {}", error), + SimpleSerializeError::Vector(error) => write!(f, "vector error: {}", error), + } + } } /// The `prelude` contains common traits and types a user of this library diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index 47964781..ecb9e2d3 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -1,12 +1,12 @@ use crate::de::{deserialize_homogeneous_composite, Deserialize, DeserializeError}; use crate::error::InstanceError; +use crate::lib::*; use crate::merkleization::{ merkleize, mix_in_length, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::lib::*; -use crate::{SimpleSerialize, Sized}; +use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 30523781..f4dd8c47 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -1,11 +1,11 @@ use crate::de::{deserialize_homogeneous_composite, Deserialize, DeserializeError}; use crate::error::{InstanceError, TypeError}; +use crate::lib::*; use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize}; -use crate::lib::*; -use crate::{SimpleSerialize, Sized}; +use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] From e53d213870e0400dfad216d2196d0774722250e7 Mon Sep 17 00:00:00 2001 From: Jun Kimura Date: Mon, 16 Jan 2023 21:49:22 +0900 Subject: [PATCH 29/45] enable `serde` feature in no_std (#8) Signed-off-by: Jun Kimura Signed-off-by: Jun Kimura --- ssz-rs/src/lib.rs | 2 +- ssz-rs/src/list.rs | 2 +- ssz-rs/src/vector.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 4b7b9016..956760cc 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -52,7 +52,7 @@ mod lib { pub use self::iter::Enumerate; #[cfg(not(feature = "std"))] - pub use alloc::{vec, vec::Vec}; + pub use alloc::{format, string::String, vec, vec::Vec}; #[cfg(feature = "std")] pub use std::vec::Vec; diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index ecb9e2d3..bc6a4d00 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -10,7 +10,7 @@ use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] -use std::marker::PhantomData; +use core::marker::PhantomData; /// A homogenous collection of a variable number of values. #[derive(Clone, Default)] diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index f4dd8c47..6386cebc 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -9,7 +9,7 @@ use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] use serde::ser::SerializeSeq; #[cfg(feature = "serde")] -use std::marker::PhantomData; +use core::marker::PhantomData; /// A homogenous collection of a fixed number of values. /// NOTE: a `Vector` of length `0` is illegal. From f47e492e601b25f361c815ee852ac1dc0b9f4273 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 17 Jan 2023 08:46:52 +0200 Subject: [PATCH 30/45] Several cleanup changes related PR comments. --- .github/workflows/rust.yml | 2 ++ ssz-rs/src/bitlist.rs | 2 +- ssz-rs/src/bitvector.rs | 2 +- ssz-rs/src/merkleization/mod.rs | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1b8e5ac2..49bd573c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -28,3 +28,5 @@ jobs: run: cargo build --all-features --verbose - name: Run tests run: cargo test --all-features --verbose + - name: Build no-std + run: cargo build --no-default-features --verbose diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index 70e0d1b2..f7d44752 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -97,7 +97,7 @@ impl Bitlist { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize_with_length(&mut data, false); + let _ = self.serialize_with_length(&mut data, false)?; pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 349f83e9..034cf923 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -103,7 +103,7 @@ impl Bitvector { fn pack_bits(&self) -> Result, MerkleizationError> { let mut data = vec![]; - let _ = self.serialize(&mut data); + let _ = self.serialize(&mut data)?; pack_bytes(&mut data); Ok(data) } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 24a3c4a5..453f5162 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -28,8 +28,8 @@ pub enum MerkleizationError { } impl From for MerkleizationError { - fn from(_: SerializeError) -> Self { - return MerkleizationError::SerializationError; + fn from(e: SerializeError) -> Self { + return MerkleizationError::SerializationError(e); } } From aaf8649d62777522835b93e7edf57ef3ad15c346 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 17 Jan 2023 08:48:15 +0200 Subject: [PATCH 31/45] Cargo fmt --- ssz-rs/src/array.rs | 2 +- ssz-rs/src/bitlist.rs | 2 +- ssz-rs/src/bitvector.rs | 2 +- ssz-rs/src/boolean.rs | 2 +- ssz-rs/src/de.rs | 2 +- ssz-rs/src/lib.rs | 10 ++++++---- ssz-rs/src/list.rs | 4 ++-- ssz-rs/src/merkleization/node.rs | 2 +- ssz-rs/src/uint.rs | 2 +- ssz-rs/src/union.rs | 2 +- ssz-rs/src/vector.rs | 4 ++-- 11 files changed, 18 insertions(+), 16 deletions(-) diff --git a/ssz-rs/src/array.rs b/ssz-rs/src/array.rs index 8e8b87e3..5d966a18 100644 --- a/ssz-rs/src/array.rs +++ b/ssz-rs/src/array.rs @@ -5,11 +5,11 @@ //! can likely be simplified to a definition over `const N: usize`. use crate::de::{deserialize_homogeneous_composite, Deserialize, DeserializeError}; use crate::error::{InstanceError, TypeError}; +use crate::lib::*; use crate::merkleization::{ merkleize, pack, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; macro_rules! define_ssz_for_array_of_size { diff --git a/ssz-rs/src/bitlist.rs b/ssz-rs/src/bitlist.rs index f7d44752..90e9a112 100644 --- a/ssz-rs/src/bitlist.rs +++ b/ssz-rs/src/bitlist.rs @@ -1,10 +1,10 @@ use crate::de::{Deserialize, DeserializeError}; use crate::error::InstanceError; +use crate::lib::*; use crate::merkleization::{ merkleize, mix_in_length, pack_bytes, MerkleizationError, Merkleized, Node, }; use crate::ser::{Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; use bitvec::prelude::{BitVec, Lsb0}; diff --git a/ssz-rs/src/bitvector.rs b/ssz-rs/src/bitvector.rs index 034cf923..c9c54ca7 100644 --- a/ssz-rs/src/bitvector.rs +++ b/ssz-rs/src/bitvector.rs @@ -1,8 +1,8 @@ use crate::de::{Deserialize, DeserializeError}; use crate::error::TypeError; +use crate::lib::*; use crate::merkleization::{merkleize, pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; use bitvec::field::BitField; use bitvec::prelude::{BitVec, Lsb0}; diff --git a/ssz-rs/src/boolean.rs b/ssz-rs/src/boolean.rs index 171c9f37..2b8ae6fc 100644 --- a/ssz-rs/src/boolean.rs +++ b/ssz-rs/src/boolean.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; +use crate::lib::*; use crate::merkleization::{MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; impl Sized for bool { diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 0f99d4ee..9bc4b4c3 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -1,6 +1,6 @@ use crate::error::{InstanceError, TypeError}; -use crate::ser::BYTES_PER_LENGTH_OFFSET; use crate::lib::*; +use crate::ser::BYTES_PER_LENGTH_OFFSET; use crate::SimpleSerialize; #[derive(Debug)] diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 956760cc..2f4fb986 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -20,6 +20,9 @@ mod union; mod utils; mod vector; +use crate::lib::*; +use crate::list::ListError; +use crate::vector::VectorError; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; @@ -30,9 +33,6 @@ pub use ser::{Serialize, SerializeError}; pub use uint::U256; pub use utils::*; pub use vector::Vector; -use crate::lib::*; -use crate::list::ListError; -use crate::vector::VectorError; mod lib { mod core { @@ -111,7 +111,9 @@ impl Display for SimpleSerializeError { match self { SimpleSerializeError::Serialize(error) => write!(f, "serialize error: {}", error), SimpleSerializeError::Deserialize(error) => write!(f, "deserialize error: {}", error), - SimpleSerializeError::Merkleization(error) => write!(f, "merkleization error: {}", error), + SimpleSerializeError::Merkleization(error) => { + write!(f, "merkleization error: {}", error) + } SimpleSerializeError::List(error) => write!(f, "list error: {}", error), SimpleSerializeError::Vector(error) => write!(f, "vector error: {}", error), } diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index bc6a4d00..0a87f03c 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -8,9 +8,9 @@ use crate::merkleization::{ use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] -use serde::ser::SerializeSeq; -#[cfg(feature = "serde")] use core::marker::PhantomData; +#[cfg(feature = "serde")] +use serde::ser::SerializeSeq; /// A homogenous collection of a variable number of values. #[derive(Clone, Default)] diff --git a/ssz-rs/src/merkleization/node.rs b/ssz-rs/src/merkleization/node.rs index 2eab8af4..722e3d99 100644 --- a/ssz-rs/src/merkleization/node.rs +++ b/ssz-rs/src/merkleization/node.rs @@ -1,5 +1,5 @@ -use crate::prelude::*; use crate::lib::*; +use crate::prelude::*; #[derive(Default, Clone, Copy, PartialEq, Eq, SimpleSerialize)] pub struct Node(pub(crate) [u8; 32]); diff --git a/ssz-rs/src/uint.rs b/ssz-rs/src/uint.rs index 5d9a81aa..76b4ece7 100644 --- a/ssz-rs/src/uint.rs +++ b/ssz-rs/src/uint.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; +use crate::lib::*; use crate::merkleization::{pack_bytes, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; use num_bigint::BigUint; diff --git a/ssz-rs/src/union.rs b/ssz-rs/src/union.rs index 6f841f2c..c920bc3f 100644 --- a/ssz-rs/src/union.rs +++ b/ssz-rs/src/union.rs @@ -1,7 +1,7 @@ use crate::de::{Deserialize, DeserializeError}; +use crate::lib::*; use crate::merkleization::{mix_in_selector, MerkleizationError, Merkleized, Node}; use crate::ser::{Serialize, SerializeError}; -use crate::lib::*; use crate::{SimpleSerialize, Sized}; /// `SimpleSerialize` is implemented for `Option` as a convenience diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 6386cebc..ba95bb91 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -7,9 +7,9 @@ use crate::merkleization::{ use crate::ser::{serialize_composite, Serialize}; use crate::{SimpleSerialize, SimpleSerializeError, Sized}; #[cfg(feature = "serde")] -use serde::ser::SerializeSeq; -#[cfg(feature = "serde")] use core::marker::PhantomData; +#[cfg(feature = "serde")] +use serde::ser::SerializeSeq; /// A homogenous collection of a fixed number of values. /// NOTE: a `Vector` of length `0` is illegal. From e8fafc87f43fd9eb425f68394a6dd4c9f21ce827 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 18 Jan 2023 08:21:02 +0200 Subject: [PATCH 32/45] Fix clippy warning --- ssz-rs/src/merkleization/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 453f5162..bedf4345 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -29,7 +29,7 @@ pub enum MerkleizationError { impl From for MerkleizationError { fn from(e: SerializeError) -> Self { - return MerkleizationError::SerializationError(e); + MerkleizationError::SerializationError(e) } } From 0da9daeed821de56e120e962aba2eddda722bd33 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 10:15:50 +0200 Subject: [PATCH 33/45] Error and display implementations cleanup. --- ssz-rs/examples/container_with_some_types.rs | 2 +- ssz-rs/src/de.rs | 4 +-- ssz-rs/src/error.rs | 25 +++++++++---------- ssz-rs/src/lib.rs | 26 +------------------- ssz-rs/src/list.rs | 2 +- ssz-rs/src/merkleization/mod.rs | 2 +- ssz-rs/src/ser.rs | 2 +- ssz-rs/src/vector.rs | 4 +-- 8 files changed, 21 insertions(+), 46 deletions(-) diff --git a/ssz-rs/examples/container_with_some_types.rs b/ssz-rs/examples/container_with_some_types.rs index c1b73b9a..b99a3d56 100644 --- a/ssz-rs/examples/container_with_some_types.rs +++ b/ssz-rs/examples/container_with_some_types.rs @@ -52,7 +52,7 @@ fn main() { let encoding = match serialize(&foo) { Ok(encoding) => encoding, Err(e) => { - eprintln!("some error encoding: {:?}", e); + eprintln!("some error encoding: {}", e); return; } }; diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 9bc4b4c3..1b70272a 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -18,14 +18,14 @@ pub enum DeserializeError { } impl Display for DeserializeError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "the value could not be deserialized: ")?; // TODO get value match *self { DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), DeserializeError::ExtraInput => { write!(f, "unexpected additional data provided when decoding") } DeserializeError::InvalidInput => write!(f, "invalid data for expected type"), - DeserializeError::IOError => write!(f, "IOError"), DeserializeError::TypeBoundsViolated { bound, len } => write!( f, "the type for this value has a bound of {} but the value has {} elements", diff --git a/ssz-rs/src/error.rs b/ssz-rs/src/error.rs index ffd0fdb9..e4a06cab 100644 --- a/ssz-rs/src/error.rs +++ b/ssz-rs/src/error.rs @@ -1,29 +1,28 @@ use crate::de::DeserializeError; use crate::merkleization::MerkleizationError; use crate::ser::SerializeError; -use thiserror::Error; // Top-level error to wrap all child errors in crate -#[derive(Debug, Error)] +#[derive(Debug)] pub enum Error { - #[error("could not serialize: {0}")] - Serialize(#[from] SerializeError), - #[error("could not deserialize: {0}")] - Deserialize(#[from] DeserializeError), - #[error("merkleization error: {0}")] - Merkleization(#[from] MerkleizationError), + // #[error("could not serialize: {0}")] + Serialize(/*#[from]*/ SerializeError), + // #[error("could not deserialize: {0}")] + Deserialize(/*#[from]*/ DeserializeError), + // #[error("merkleization error: {0}")] + Merkleization(/*#[from]*/ MerkleizationError), } -#[derive(Error, Debug)] +#[derive(Debug)] pub enum TypeError { - #[error("the type for this value is invalid with bound {0}")] + // #[error("the type for this value is invalid with bound {0}")] InvalidBound(usize), } -#[derive(Error, Debug)] +#[derive(Debug)] pub enum InstanceError { - #[error("required {required} elements for this type but {provided} elements given")] + // #[error("required {required} elements for this type but {provided} elements given")] Exact { required: usize, provided: usize }, - #[error("{provided} elements given for a type with (inclusive) upper bound {bound}")] + // #[error("{provided} elements given for a type with (inclusive) upper bound {bound}")] Bounded { bound: usize, provided: usize }, } diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 2f4fb986..2c79c6b3 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -21,8 +21,6 @@ mod utils; mod vector; use crate::lib::*; -use crate::list::ListError; -use crate::vector::VectorError; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; @@ -30,6 +28,7 @@ pub use error::Error; pub use list::List; pub use merkleization::{Context as MerkleizationContext, MerkleizationError, Merkleized, Node}; pub use ser::{Serialize, SerializeError}; +use std::vec; pub use uint::U256; pub use utils::*; pub use vector::Vector; @@ -97,29 +96,6 @@ where T::deserialize(encoding) } -#[derive(Debug)] -pub enum SimpleSerializeError { - Serialize(SerializeError), - Deserialize(DeserializeError), - Merkleization(MerkleizationError), - List(ListError), - Vector(VectorError), -} - -impl Display for SimpleSerializeError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - match self { - SimpleSerializeError::Serialize(error) => write!(f, "serialize error: {}", error), - SimpleSerializeError::Deserialize(error) => write!(f, "deserialize error: {}", error), - SimpleSerializeError::Merkleization(error) => { - write!(f, "merkleization error: {}", error) - } - SimpleSerializeError::List(error) => write!(f, "list error: {}", error), - SimpleSerializeError::Vector(error) => write!(f, "vector error: {}", error), - } - } -} - /// The `prelude` contains common traits and types a user of this library /// would want to have handy with a simple (single) import. pub mod prelude { diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index 0a87f03c..c8495149 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -6,7 +6,7 @@ use crate::merkleization::{ BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize, SerializeError}; -use crate::{SimpleSerialize, SimpleSerializeError, Sized}; +use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use core::marker::PhantomData; #[cfg(feature = "serde")] diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index bedf4345..5b834b8a 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -34,7 +34,7 @@ impl From for MerkleizationError { } impl Display for MerkleizationError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::SerializationError(err) => { write!(f, "failed to serialize value: {err}") diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index 18f57dc4..ade90cc6 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -17,7 +17,7 @@ pub enum SerializeError { } impl Display for SerializeError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match *self { SerializeError::MaximumEncodedLengthExceeded(size) => write!( f, diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index ba95bb91..9b64cd05 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -5,7 +5,7 @@ use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; use crate::ser::{serialize_composite, Serialize}; -use crate::{SimpleSerialize, SimpleSerializeError, Sized}; +use crate::{SerializeError, SimpleSerialize, Sized}; #[cfg(feature = "serde")] use core::marker::PhantomData; #[cfg(feature = "serde")] @@ -20,7 +20,7 @@ pub struct Vector { } impl Display for Error { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match *self { Error::IncorrectLengthVector { expected, provided } => write!( f, From 8545f60dbeae878370137e672da2d63661bc2db3 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 10:31:03 +0200 Subject: [PATCH 34/45] Error cleanup. --- ssz-rs/src/de.rs | 2 +- ssz-rs/src/lib.rs | 24 ------------------------ ssz-rs/src/merkleization/mod.rs | 2 +- ssz-rs/src/ser.rs | 3 ++- ssz-rs/src/vector.rs | 6 +++--- 5 files changed, 7 insertions(+), 30 deletions(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index 1b70272a..eac1e1ab 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -20,7 +20,7 @@ pub enum DeserializeError { impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "the value could not be deserialized: ")?; // TODO get value - match *self { + match self { DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), DeserializeError::ExtraInput => { write!(f, "unexpected additional data provided when decoding") diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index 2c79c6b3..be8bcaac 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -20,7 +20,6 @@ mod union; mod utils; mod vector; -use crate::lib::*; pub use bitlist::Bitlist; pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; @@ -28,7 +27,6 @@ pub use error::Error; pub use list::List; pub use merkleization::{Context as MerkleizationContext, MerkleizationError, Merkleized, Node}; pub use ser::{Serialize, SerializeError}; -use std::vec; pub use uint::U256; pub use utils::*; pub use vector::Vector; @@ -74,28 +72,6 @@ pub trait SimpleSerialize: Serialize + Deserialize + Sized + Merkleized + Defaul } } -/// `serialize` is a convenience function for taking a value that -/// implements `SimpleSerialize` and attempting to encode it to -/// a `Vec` according to the SSZ spec. -pub fn serialize(value: &T) -> Result, SerializeError> -where - T: SimpleSerialize, -{ - let mut result = vec![]; - value.serialize(&mut result)?; - Ok(result) -} - -/// `deserialize` is a convenience function for taking an encoding -/// for some value that implements `SimpleSerialize` in a `&[u8]` -/// and attempting to deserialize that value from the byte representation. -pub fn deserialize(encoding: &[u8]) -> Result -where - T: SimpleSerialize, -{ - T::deserialize(encoding) -} - /// The `prelude` contains common traits and types a user of this library /// would want to have handy with a simple (single) import. pub mod prelude { diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 5b834b8a..1ebb17b5 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -7,7 +7,6 @@ use crate::ser::{Serialize, SerializeError}; use lazy_static::lazy_static; use sha2::{Digest, Sha256}; -use crate::SerializeError; pub use cache::Cache as MerkleCache; pub use node::Node; pub use proofs::is_valid_merkle_branch; @@ -35,6 +34,7 @@ impl From for MerkleizationError { impl Display for MerkleizationError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "the value could not be merkleized: ")?; match self { Self::SerializationError(err) => { write!(f, "failed to serialize value: {err}") diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index ade90cc6..5b8d306f 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -18,7 +18,8 @@ pub enum SerializeError { impl Display for SerializeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match *self { + write!(f, "the value could not be serialized: ")?; + match self { SerializeError::MaximumEncodedLengthExceeded(size) => write!( f, "the encoded length is {} which exceeds the maximum length {}", diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index 9b64cd05..bebf4e21 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -21,10 +21,10 @@ pub struct Vector { impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match *self { - Error::IncorrectLengthVector { expected, provided } => write!( + match self { + Error::IncorrectLength { expected, provided } => write!( f, - "incorrect number of elements {} to make a Vector of length {}", + "{} elements given that exceeds the length bound of the Vector of {}", provided, expected ), } From 090e9138d5150e8f68c08db03d8817f84b00d05f Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 10:38:43 +0200 Subject: [PATCH 35/45] Adds lib dependency for utils. --- ssz-rs/src/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ssz-rs/src/utils.rs b/ssz-rs/src/utils.rs index d18f1705..c708d1fb 100644 --- a/ssz-rs/src/utils.rs +++ b/ssz-rs/src/utils.rs @@ -1,4 +1,5 @@ use crate::de::DeserializeError; +use crate::lib::*; use crate::ser::SerializeError; use crate::SimpleSerialize; From 832d40b8e1d77d909e489934f827206357fd7488 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 10:48:12 +0200 Subject: [PATCH 36/45] Removes comment. --- ssz-rs/src/de.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index eac1e1ab..ce3c76bf 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -19,7 +19,7 @@ pub enum DeserializeError { impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "the value could not be deserialized: ")?; // TODO get value + write!(f, "the value could not be deserialized: ")?; match self { DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), DeserializeError::ExtraInput => { From 945c827048f984a7ec03d56969e6fdb324cbe27a Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 10:55:09 +0200 Subject: [PATCH 37/45] Revert import. --- ssz-rs/src/vector.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index bebf4e21..ef5c9de9 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -4,8 +4,8 @@ use crate::lib::*; use crate::merkleization::{ merkleize, pack, MerkleCache, MerkleizationError, Merkleized, Node, BYTES_PER_CHUNK, }; -use crate::ser::{serialize_composite, Serialize}; -use crate::{SerializeError, SimpleSerialize, Sized}; +use crate::ser::{serialize_composite, Serialize, SerializeError}; +use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] use core::marker::PhantomData; #[cfg(feature = "serde")] From 1392dbc267cd97daa8976c3c9dbed4701151ee9a Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Thu, 19 Jan 2023 11:05:30 +0200 Subject: [PATCH 38/45] Cargo.toml cleanup. --- ssz-rs/Cargo.toml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 37a8f27e..7440fe51 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -18,10 +18,9 @@ exclude = [ [features] default = ["serde", "std"] std = [ - "bitvec/std", - "sha2/std", - "num-bigint/std", - "serde/std" + "bitvec/default", + "sha2/default", + "num-bigint/default", ] serde = ["dep:serde", "dep:hex"] @@ -30,7 +29,7 @@ thiserror = "1.0.25" bitvec = { version = "1.0.0", default-features = false, features=["alloc"]} ssz-rs-derive = { path = "../ssz-rs-derive"} sha2 = { version ="0.9.8", default-features = false} -lazy_static = { version = "1.4.0", features = ["spin_no_std"]} +lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"]} serde = { version = "1.0", features = ["derive"], optional = true } hex = {version = "0.4.3", optional = true } num-bigint = { version ="0.4.3", default-features = false} From 4f1925330495707c6e461d7eab6fb0396827486e Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Sun, 22 Jan 2023 17:42:12 +0100 Subject: [PATCH 39/45] integrate new errors into `no-std` world --- ssz-rs/src/de.rs | 37 ++++++++++++++-------------- ssz-rs/src/error.rs | 57 +++++++++++++++++++++++++++++++++++++++++--- ssz-rs/src/ser.rs | 28 ++++++++++++---------- ssz-rs/src/vector.rs | 12 ---------- 4 files changed, 87 insertions(+), 47 deletions(-) diff --git a/ssz-rs/src/de.rs b/ssz-rs/src/de.rs index ce3c76bf..f86aff8c 100644 --- a/ssz-rs/src/de.rs +++ b/ssz-rs/src/de.rs @@ -5,37 +5,36 @@ use crate::SimpleSerialize; #[derive(Debug)] pub enum DeserializeError { - // #[error("expected at least {expected} bytes when decoding but provided only {provided} bytes")] ExpectedFurtherInput { provided: usize, expected: usize }, - // #[error("{provided} bytes given but only expected {expected} bytes")] AdditionalInput { provided: usize, expected: usize }, - // #[error("invalid byte {0:x} when decoding data of the expected type")] InvalidByte(u8), - // #[error("invalid instance: {0}")] InvalidInstance(InstanceError), - // #[error("invalid type: {0}")] InvalidType(TypeError), } +impl From for DeserializeError { + fn from(err: InstanceError) -> Self { + Self::InvalidInstance(err) + } +} + +impl From for DeserializeError { + fn from(err: TypeError) -> Self { + Self::InvalidType(err) + } +} + impl Display for DeserializeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "the value could not be deserialized: ")?; match self { - DeserializeError::InputTooShort => write!(f, "expected further data when decoding"), - DeserializeError::ExtraInput => { - write!(f, "unexpected additional data provided when decoding") - } - DeserializeError::InvalidInput => write!(f, "invalid data for expected type"), - DeserializeError::TypeBoundsViolated { bound, len } => write!( - f, - "the type for this value has a bound of {} but the value has {} elements", - bound, len - ), - DeserializeError::IllegalType { bound } => write!( + DeserializeError::ExpectedFurtherInput { provided, expected } => write!(f, "expected at least {expected} bytes when decoding but provided only {provided} bytes"), + DeserializeError::AdditionalInput { provided, expected } => write!(f, "{provided} bytes given but only expected {expected} bytes"), + DeserializeError::InvalidByte(b) => write!( f, - "the type for this value has an illegal bound of {}", - bound + "invalid byte {b:x} when decoding data of the expected type" ), + DeserializeError::InvalidInstance(err) => write!(f, "invalid instance: {err}"), + DeserializeError::InvalidType(err) => write!(f, "invalid type: {err}"), } } } diff --git a/ssz-rs/src/error.rs b/ssz-rs/src/error.rs index e4a06cab..5f18a44f 100644 --- a/ssz-rs/src/error.rs +++ b/ssz-rs/src/error.rs @@ -1,4 +1,5 @@ use crate::de::DeserializeError; +use crate::lib::*; use crate::merkleization::MerkleizationError; use crate::ser::SerializeError; @@ -13,16 +14,66 @@ pub enum Error { Merkleization(/*#[from]*/ MerkleizationError), } +impl From for Error { + fn from(err: SerializeError) -> Self { + Self::Serialize(err) + } +} + +impl From for Error { + fn from(err: DeserializeError) -> Self { + Self::Deserialize(err) + } +} + +impl From for Error { + fn from(err: MerkleizationError) -> Self { + Self::Merkleization(err) + } +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Serialize(err) => write!(f, "could not serialize: {err}"), + Self::Deserialize(err) => write!(f, "could not deserialize: {err}"), + Self::Merkleization(err) => write!(f, "merkleization error: {err}"), + } + } +} + #[derive(Debug)] pub enum TypeError { - // #[error("the type for this value is invalid with bound {0}")] InvalidBound(usize), } +impl Display for TypeError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidBound(size) => { + write!(f, "the type for this value is invalid with bound {size}") + } + } + } +} + #[derive(Debug)] pub enum InstanceError { - // #[error("required {required} elements for this type but {provided} elements given")] Exact { required: usize, provided: usize }, - // #[error("{provided} elements given for a type with (inclusive) upper bound {bound}")] Bounded { bound: usize, provided: usize }, } + +impl Display for InstanceError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Exact { required, provided } => write!( + f, + "required {required} elements for this type but {provided} elements given" + ), + Self::Bounded { bound, provided } => write!( + f, + "{provided} elements given for a type with (inclusive) upper bound {bound}" + ), + } + } +} diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index 5b8d306f..f23b8e2c 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -16,25 +16,27 @@ pub enum SerializeError { InvalidType(/*#[from]*/ TypeError), } +impl From for SerializeError { + fn from(err: InstanceError) -> Self { + Self::InvalidInstance(err) + } +} + +impl From for SerializeError { + fn from(err: TypeError) -> Self { + Self::InvalidType(err) + } +} + impl Display for SerializeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "the value could not be serialized: ")?; match self { SerializeError::MaximumEncodedLengthExceeded(size) => write!( f, - "the encoded length is {} which exceeds the maximum length {}", - size, MAXIMUM_LENGTH - ), - SerializeError::TypeBoundsViolated { bound, len } => write!( - f, - "the type for this value has a bound of {} but the value has {} elements", - bound, len - ), - SerializeError::IllegalType { bound } => write!( - f, - "the type for this value has an illegal bound of {}", - bound + "the encoded length is {size} which exceeds the maximum length {MAXIMUM_LENGTH}", ), + SerializeError::InvalidInstance(err) => write!(f, "invalid instance: {err}"), + SerializeError::InvalidType(err) => write!(f, "invalid type: {err}"), } } } diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index ef5c9de9..b821ab83 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -19,18 +19,6 @@ pub struct Vector { cache: MerkleCache, } -impl Display for Error { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - Error::IncorrectLength { expected, provided } => write!( - f, - "{} elements given that exceeds the length bound of the Vector of {}", - provided, expected - ), - } - } -} - #[cfg(feature = "serde")] impl serde::Serialize for Vector { fn serialize(&self, serializer: S) -> Result From 0a451aedd19fd6ba698ac7d2f0b628d380760ba0 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 09:59:41 +0100 Subject: [PATCH 40/45] formatting --- ssz-rs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 7440fe51..9b9ccdac 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -26,7 +26,7 @@ serde = ["dep:serde", "dep:hex"] [dependencies] thiserror = "1.0.25" -bitvec = { version = "1.0.0", default-features = false, features=["alloc"]} +bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] } ssz-rs-derive = { path = "../ssz-rs-derive"} sha2 = { version ="0.9.8", default-features = false} lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"]} From 3effc831db1f8697f0cd5bab0fd42052bf7aa8a0 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 09:59:49 +0100 Subject: [PATCH 41/45] remove redundant flag --- ssz-rs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz-rs/Cargo.toml b/ssz-rs/Cargo.toml index 9b9ccdac..3deeb7f5 100644 --- a/ssz-rs/Cargo.toml +++ b/ssz-rs/Cargo.toml @@ -29,7 +29,7 @@ thiserror = "1.0.25" bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] } ssz-rs-derive = { path = "../ssz-rs-derive"} sha2 = { version ="0.9.8", default-features = false} -lazy_static = { version = "1.4.0", default-features = false, features = ["spin_no_std"]} +lazy_static = { version = "1.4.0", features = ["spin_no_std"] } serde = { version = "1.0", features = ["derive"], optional = true } hex = {version = "0.4.3", optional = true } num-bigint = { version ="0.4.3", default-features = false} From 75740188c5a6cca2439195d578ca9cfb61c033a1 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 11:55:02 +0100 Subject: [PATCH 42/45] clean up error refactor --- ssz-rs/src/error.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/ssz-rs/src/error.rs b/ssz-rs/src/error.rs index 5f18a44f..38082d7d 100644 --- a/ssz-rs/src/error.rs +++ b/ssz-rs/src/error.rs @@ -6,11 +6,8 @@ use crate::ser::SerializeError; // Top-level error to wrap all child errors in crate #[derive(Debug)] pub enum Error { - // #[error("could not serialize: {0}")] Serialize(/*#[from]*/ SerializeError), - // #[error("could not deserialize: {0}")] Deserialize(/*#[from]*/ DeserializeError), - // #[error("merkleization error: {0}")] Merkleization(/*#[from]*/ MerkleizationError), } From 1a2c446dc2625e3e679c57e36de554a7165684ad Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 11:55:14 +0100 Subject: [PATCH 43/45] clean up `prelude` export --- ssz-rs/src/lib.rs | 4 ++-- ssz-rs/src/merkleization/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index be8bcaac..d109c2c8 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -25,7 +25,7 @@ pub use bitvector::Bitvector; pub use de::{Deserialize, DeserializeError}; pub use error::Error; pub use list::List; -pub use merkleization::{Context as MerkleizationContext, MerkleizationError, Merkleized, Node}; +pub use merkleization::{MerkleizationError, Merkleized, Node}; pub use ser::{Serialize, SerializeError}; pub use uint::U256; pub use utils::*; @@ -80,6 +80,7 @@ pub mod prelude { pub use crate::bitvector::Bitvector; pub use crate::de::Deserialize; pub use crate::de::DeserializeError; + pub use crate::error::{InstanceError, TypeError}; pub use crate::list::List; pub use crate::merkleization::{ is_valid_merkle_branch, merkleize, mix_in_selector, pack, pack_bytes, MerkleizationError, @@ -90,7 +91,6 @@ pub mod prelude { pub use crate::utils::{deserialize, serialize}; pub use crate::vector::Vector; pub use crate::Error as SimpleSerializeError; - pub use crate::MerkleizationContext; pub use crate::SimpleSerialize; pub use crate::Sized; pub use ssz_rs_derive::SimpleSerialize; diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 1100053a..cd8796ea 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -78,7 +78,7 @@ fn hash_nodes(hasher: &mut Sha256, a: &[u8], b: &[u8], out: &mut [u8]) { const MAX_MERKLE_TREE_DEPTH: usize = 64; #[derive(Debug)] -pub struct Context { +struct Context { zero_hashes: [u8; MAX_MERKLE_TREE_DEPTH * BYTES_PER_CHUNK], } From 3bd29909d385f997785d4f6dfbca0da75f909495 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 12:15:05 +0100 Subject: [PATCH 44/45] misc cleanups --- ssz-rs/src/error.rs | 6 +++--- ssz-rs/src/lib.rs | 4 ++-- ssz-rs/src/list.rs | 6 +++--- ssz-rs/src/merkleization/mod.rs | 10 ++-------- ssz-rs/src/ser.rs | 7 ++----- ssz-rs/src/vector.rs | 4 ++-- 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/ssz-rs/src/error.rs b/ssz-rs/src/error.rs index 38082d7d..573b0c57 100644 --- a/ssz-rs/src/error.rs +++ b/ssz-rs/src/error.rs @@ -6,9 +6,9 @@ use crate::ser::SerializeError; // Top-level error to wrap all child errors in crate #[derive(Debug)] pub enum Error { - Serialize(/*#[from]*/ SerializeError), - Deserialize(/*#[from]*/ DeserializeError), - Merkleization(/*#[from]*/ MerkleizationError), + Serialize(SerializeError), + Deserialize(DeserializeError), + Merkleization(MerkleizationError), } impl From for Error { diff --git a/ssz-rs/src/lib.rs b/ssz-rs/src/lib.rs index d109c2c8..b6c2a692 100644 --- a/ssz-rs/src/lib.rs +++ b/ssz-rs/src/lib.rs @@ -39,11 +39,11 @@ mod lib { pub use std::*; } - pub use self::core::{any, cmp, iter, slice}; + pub use self::core::{any, cmp, fmt, iter, slice}; pub use self::cmp::Ordering; pub use self::core::array::TryFromSliceError; - pub use self::core::fmt::{self, Debug, Display, Formatter}; + pub use self::core::fmt::{Debug, Display, Formatter}; pub use self::core::ops::{Deref, DerefMut, Index, IndexMut}; pub use self::core::slice::{IterMut, SliceIndex}; pub use self::iter::Enumerate; diff --git a/ssz-rs/src/list.rs b/ssz-rs/src/list.rs index c8495149..69256c75 100644 --- a/ssz-rs/src/list.rs +++ b/ssz-rs/src/list.rs @@ -8,9 +8,9 @@ use crate::merkleization::{ use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] -use core::marker::PhantomData; -#[cfg(feature = "serde")] use serde::ser::SerializeSeq; +#[cfg(feature = "serde")] +use std::marker::PhantomData; /// A homogenous collection of a variable number of values. #[derive(Clone, Default)] @@ -287,7 +287,7 @@ pub struct IterMut<'a, T, const N: usize> where T: SimpleSerialize, { - inner: Enumerate>, + inner: Enumerate>, cache: &'a mut MerkleCache, } diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index cd8796ea..050e4b8a 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -19,9 +19,7 @@ pub trait Merkleized { #[derive(Debug)] pub enum MerkleizationError { - // #[error("failed to serialize value: {0}")] - SerializationError(/*#[from]*/ SerializeError), - // #[error("cannot merkleize data that exceeds the declared limit {0}")] + SerializationError(SerializeError), InputExceedsLimit(usize), } @@ -33,15 +31,11 @@ impl From for MerkleizationError { impl Display for MerkleizationError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "the value could not be merkleized: ")?; match self { Self::SerializationError(err) => { write!(f, "failed to serialize value: {err}") } - Self::InputExceedsLimit(size) => write!( - f, - "cannot merkleize data that exceeds the declared limit: {size}", - ), + Self::InputExceedsLimit(size) => write!(f, "data exceeds the declared limit {size}"), } } } diff --git a/ssz-rs/src/ser.rs b/ssz-rs/src/ser.rs index f23b8e2c..352286f7 100644 --- a/ssz-rs/src/ser.rs +++ b/ssz-rs/src/ser.rs @@ -8,12 +8,9 @@ const MAXIMUM_LENGTH: u64 = 2u64.pow((8 * BYTES_PER_LENGTH_OFFSET) as u32); #[derive(Debug)] pub enum SerializeError { - // #[error("the encoded length is {0} which exceeds the maximum length {MAXIMUM_LENGTH}")] MaximumEncodedLengthExceeded(usize), - // #[error("invalid instance: {0}")] - InvalidInstance(/*#[from]*/ InstanceError), - // #[error("invalid type: {0}")] - InvalidType(/*#[from]*/ TypeError), + InvalidInstance(InstanceError), + InvalidType(TypeError), } impl From for SerializeError { diff --git a/ssz-rs/src/vector.rs b/ssz-rs/src/vector.rs index b821ab83..a2ba50c4 100644 --- a/ssz-rs/src/vector.rs +++ b/ssz-rs/src/vector.rs @@ -7,9 +7,9 @@ use crate::merkleization::{ use crate::ser::{serialize_composite, Serialize, SerializeError}; use crate::{SimpleSerialize, Sized}; #[cfg(feature = "serde")] -use core::marker::PhantomData; -#[cfg(feature = "serde")] use serde::ser::SerializeSeq; +#[cfg(feature = "serde")] +use std::marker::PhantomData; /// A homogenous collection of a fixed number of values. /// NOTE: a `Vector` of length `0` is illegal. From 64fed645fccd1fa00dddf620d607c002ad65c029 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 23 Jan 2023 14:33:14 +0100 Subject: [PATCH 45/45] Update ssz-rs/src/merkleization/mod.rs --- ssz-rs/src/merkleization/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ssz-rs/src/merkleization/mod.rs b/ssz-rs/src/merkleization/mod.rs index 050e4b8a..b5a969c0 100644 --- a/ssz-rs/src/merkleization/mod.rs +++ b/ssz-rs/src/merkleization/mod.rs @@ -24,8 +24,8 @@ pub enum MerkleizationError { } impl From for MerkleizationError { - fn from(e: SerializeError) -> Self { - MerkleizationError::SerializationError(e) + fn from(err: SerializeError) -> Self { + MerkleizationError::SerializationError(err) } }