Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] bincode deserialization fails for fixed-size byte arrays #221

Closed
Wollac opened this issue Aug 2, 2023 · 1 comment · Fixed by #223
Closed

[Bug] bincode deserialization fails for fixed-size byte arrays #221

Wollac opened this issue Aug 2, 2023 · 1 comment · Fixed by #223
Labels
bug Something isn't working

Comments

@Wollac
Copy link
Contributor

Wollac commented Aug 2, 2023

Component

primitives

What version of Alloy are you on?

alloy-primitives v0.3.1

Operating System

None

Describe the bug

Trying to use bincode to deserialize FixedBytes<N> fails because deserializer.deserialize_any is not supported.

Code snippet to reproduce this bug:

let hash = B256::ZERO;
bincode::deserialize::<B256>(&bincode::serialize(&hash).unwrap()).unwrap();

The serde documentation also discourages the use of deserialize_any for this very reason:

When implementing Deserialize, you should avoid relying on Deserializer::deserialize_any unless you need to be told by the Deserializer what type is in the input. Know that relying on Deserializer::deserialize_any means your data type will be able to deserialize from self-describing formats only, ruling out Postcard and many others.

However, as I understand #202, JSON deserialization support for either a byte array, byte sequence, or string was explicitly required. Here I see a possible compromise to also check is_human_readable() in the deserialization and keep the current parsing only for human readable, e.g. JSON, and use deserializer.deserialize_bytes otherwise, which would also allow bincode and similar formats.

If this approach is acceptable, I would be happy to provide a PR.

@Wollac Wollac added the bug Something isn't working label Aug 2, 2023
@prestwich
Copy link
Member

approach sounds good to me, thanks ✔️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants