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

feat: manual blob deserialize #696

Merged
merged 3 commits into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions crates/eips/src/eip4844/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ use alloc::vec::Vec;
)]
pub struct BlobTransactionSidecar {
/// The blob data.
#[cfg_attr(
all(debug_assertions, feature = "serde"),
serde(deserialize_with = "deserialize_blobs")
)]
pub blobs: Vec<Blob>,
/// The blob commitments.
pub commitments: Vec<Bytes48>,
Expand Down Expand Up @@ -183,6 +187,22 @@ impl Decodable for BlobTransactionSidecar {
}
}

// Helper function to deserialize boxed blobs
#[cfg(all(debug_assertions, feature = "serde"))]
fn deserialize_blobs<'de, D>(deserializer: D) -> Result<Vec<Blob>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
use serde::Deserialize;

let raw_blobs = Vec::<alloy_primitives::Bytes>::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")]
Expand Down Expand Up @@ -244,3 +264,31 @@ impl From<c_kzg::Error> 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);
}
}
Loading