Closed DragonDev1906 closed 11 months ago
it seems that this is only an issue w/ bincode
and this repo doesn't aim to support any possible serialization scheme, just ssz
.
if you try your example w/ just the relevant SimpleSerialize
and serde
traits, there is no problem with encoding/decoding
it looks like there are specific bincode
traits to derive Encode
and Decode
-- do you still have this problem if you derive those on your type?
if you try your example w/ just the relevant SimpleSerialize and serde traits, there is no problem with encoding/decoding
bincode
just uses serde and its derived types. I assume there is no issue when using other serde formats (e.g. serde_json) because they don't rely on the size for deserialization.
it looks like there are specific bincode traits to derive Encode and Decode
Not that I know of and I couldn't find specific traits in bincode
. As mentioned above: As far as I can tell bincode
just uses the serde types.
In fact, when I tell the serde derive macro to set the correct length everything works as expected:
pub mod sszlist_hotfix {
use serde::ser::SerializeSeq;
use ssz_rs::List;
pub fn serialize<S, T, const N: usize>(
value: &List<T, N>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
T: ssz_rs::SimpleSerialize + serde::Serialize,
{
let mut seq = serializer.serialize_seq(Some(value.len()))?;
for element in value.iter() {
seq.serialize_element(element)?;
}
seq.end()
}
The following is the serde::Serialize
implementation for List<T, N>
in ssz-rs
. According to the documentation of serde, this argument is not the max size but the actual amount of elements.
#[cfg(feature = "serde")]
impl<T: SimpleSerialize + serde::Serialize, const N: usize> serde::Serialize for List<T, N> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(N))?; // <---- Bug?
for element in &self.data {
seq.serialize_element(element)?;
}
seq.end()
}
}
The argument is the number of elements in the sequence, which may or may not be computable before the sequence is iterated.
Not that I know of and I couldn't find specific traits
ah sorry, looks like this is in version 2 of bincode
and im not sure what the difference is
his argument is not the max size but the actual amount of elements.
yeah, I see this and I think you are correct this is a bug! thanks for raising this issue
I'll triple-check and go fix today if its an issue
@DragonDev1906 this should work now after #111
let me know if you have any trouble
The
List
type does not correctly serialize its size (serde), thus breaking any serialization implementation (tested with bincode) that requires a correct length. The test below serializes and then immediately deserializes a List of length 5 with a maximum size of 32, but the serialized data contains the length2000000000000000
(32), thus breaking the deserialization.