From 6a087061d72e8115abdede09cdff9f0ed859fa5c Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Tue, 14 May 2024 16:13:23 +0200 Subject: [PATCH 1/2] fix(basic_types): bincode deserialization for `web3::Bytes` Don't assume we only use serde_json... Signed-off-by: Harald Hoyer --- core/lib/basic_types/src/web3/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/lib/basic_types/src/web3/mod.rs b/core/lib/basic_types/src/web3/mod.rs index be785040d2d..a2166d62710 100644 --- a/core/lib/basic_types/src/web3/mod.rs +++ b/core/lib/basic_types/src/web3/mod.rs @@ -61,9 +61,13 @@ impl Serialize for Bytes { where S: Serializer, { - let mut serialized = "0x".to_owned(); - serialized.push_str(&hex::encode(&self.0)); - serializer.serialize_str(serialized.as_ref()) + if serializer.is_human_readable() { + let mut serialized = "0x".to_owned(); + serialized.push_str(&hex::encode(&self.0)); + serializer.serialize_str(serialized.as_ref()) + } else { + serializer.serialize_bytes(&self.0) + } } } @@ -72,7 +76,11 @@ impl<'a> Deserialize<'a> for Bytes { where D: Deserializer<'a>, { - deserializer.deserialize_identifier(BytesVisitor) + if deserializer.is_human_readable() { + deserializer.deserialize_identifier(BytesVisitor) + } else { + Vec::::deserialize(deserializer).map(Bytes) + } } } From a43e3d1acb11bd94113dc675a3e204737ccfb5f4 Mon Sep 17 00:00:00 2001 From: Patrick Beza Date: Tue, 14 May 2024 17:44:09 +0200 Subject: [PATCH 2/2] Add a [de]serialization unit test for the `Bytes` --- core/lib/basic_types/src/web3/tests.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/lib/basic_types/src/web3/tests.rs b/core/lib/basic_types/src/web3/tests.rs index e7c0a0d048a..bbeaece0374 100644 --- a/core/lib/basic_types/src/web3/tests.rs +++ b/core/lib/basic_types/src/web3/tests.rs @@ -83,3 +83,12 @@ fn block_can_be_deserialized() { let block: Block = serde_json::from_str(pre_dencun).unwrap(); assert!(block.excess_blob_gas.is_none()); } + +#[test] +fn test_bytes_serde() { + use bincode::{deserialize, serialize}; + let original = Bytes(vec![0, 1, 2, 3, 4]); + let encoded: Vec = serialize(&original).unwrap(); + let decoded: Bytes = deserialize(&encoded).unwrap(); + assert_eq!(original, decoded); +}