From c0358b17d88ace1d4db08a9d88a68f50c777831e Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 6 May 2024 14:30:05 +0200 Subject: [PATCH 1/3] feat: manual blob deserialize --- crates/eips/src/eip4844/sidecar.rs | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/eips/src/eip4844/sidecar.rs b/crates/eips/src/eip4844/sidecar.rs index 0a17968cbdd..ff891235e57 100644 --- a/crates/eips/src/eip4844/sidecar.rs +++ b/crates/eips/src/eip4844/sidecar.rs @@ -21,6 +21,7 @@ use alloc::vec::Vec; )] pub struct BlobTransactionSidecar { /// The blob data. + #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_blobs"))] pub blobs: Vec, /// The blob commitments. pub commitments: Vec, @@ -183,6 +184,22 @@ impl Decodable for BlobTransactionSidecar { } } +// Helper function to deserialize boxed blobs +#[cfg(feature = "serde")] +fn deserialize_blobs<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::de::Deserializer<'de>, +{ + use serde::Deserialize; + + let raw_blobs = Vec::::deserialize(deserializer)?; + let mut blobs = Vec::with_capacity(raw_blobs.len()); + for blob in raw_blobs { + blobs.push(Blob::try_from(blob.as_ref()).map_err(serde::de::Error::custom)?); + } + Ok(blobs) +} + /// An error that can occur when validating a [BlobTransactionSidecar::validate]. #[derive(Debug)] #[cfg(feature = "kzg")] @@ -244,3 +261,31 @@ impl From for BlobTransactionValidationError { BlobTransactionValidationError::KZGError(source) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[cfg(feature = "serde")] + fn deserialize_blob() { + let blob = BlobTransactionSidecar { + blobs: vec![Blob::default(), Blob::default(), Blob::default(), Blob::default()], + commitments: vec![ + Bytes48::default(), + Bytes48::default(), + Bytes48::default(), + Bytes48::default(), + ], + proofs: vec![ + Bytes48::default(), + Bytes48::default(), + Bytes48::default(), + Bytes48::default(), + ], + }; + let s = serde_json::to_string(&blob).unwrap(); + let deserialized: BlobTransactionSidecar = serde_json::from_str(&s).unwrap(); + assert_eq!(blob, deserialized); + } +} From ad401463ed812d5bbdf055fbc7ec44b6f19ac45f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 6 May 2024 17:03:05 +0200 Subject: [PATCH 2/3] use debug_assertions --- crates/eips/src/eip4844/sidecar.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/eips/src/eip4844/sidecar.rs b/crates/eips/src/eip4844/sidecar.rs index ff891235e57..b0644af4102 100644 --- a/crates/eips/src/eip4844/sidecar.rs +++ b/crates/eips/src/eip4844/sidecar.rs @@ -21,7 +21,10 @@ use alloc::vec::Vec; )] pub struct BlobTransactionSidecar { /// The blob data. - #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_blobs"))] + #[cfg_attr( + all(debug_assertions, feature = "serde"), + serde(deserialize_with = "deserialize_blobs") + )] pub blobs: Vec, /// The blob commitments. pub commitments: Vec, From 5c75d5fe239ae7abb86b6aae595d0958a686b62c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 6 May 2024 17:21:16 +0200 Subject: [PATCH 3/3] Update crates/eips/src/eip4844/sidecar.rs Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/eips/src/eip4844/sidecar.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eips/src/eip4844/sidecar.rs b/crates/eips/src/eip4844/sidecar.rs index b0644af4102..e8d18ef90c3 100644 --- a/crates/eips/src/eip4844/sidecar.rs +++ b/crates/eips/src/eip4844/sidecar.rs @@ -188,7 +188,7 @@ impl Decodable for BlobTransactionSidecar { } // Helper function to deserialize boxed blobs -#[cfg(feature = "serde")] +#[cfg(all(debug_assertions, feature = "serde"))] fn deserialize_blobs<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::de::Deserializer<'de>,