Skip to content

Commit

Permalink
fix: flat serde for Sealed
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Oct 16, 2024
1 parent 670b39a commit 23a5505
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,15 @@ mod serde_from {
#[serde(tag = "type")]
pub(crate) enum TaggedTxEnvelope {
#[serde(rename = "0x0", alias = "0x00")]
Legacy(Enveloped<TxLegacy>),
Legacy(#[serde(with = "alloy_serde::sealed::flat_hash")] Enveloped<TxLegacy>),
#[serde(rename = "0x1", alias = "0x01")]
Eip2930(Enveloped<TxEip2930>),
Eip2930(#[serde(with = "alloy_serde::sealed::flat_hash")] Enveloped<TxEip2930>),
#[serde(rename = "0x2", alias = "0x02")]
Eip1559(Enveloped<TxEip1559>),
Eip1559(#[serde(with = "alloy_serde::sealed::flat_hash")] Enveloped<TxEip1559>),
#[serde(rename = "0x3", alias = "0x03")]
Eip4844(Enveloped<TxEip4844Variant>),
Eip4844(#[serde(with = "alloy_serde::sealed::flat_hash")] Enveloped<TxEip4844Variant>),
#[serde(rename = "0x4", alias = "0x04")]
Eip7702(Enveloped<TxEip7702>),
Eip7702(#[serde(with = "alloy_serde::sealed::flat_hash")] Enveloped<TxEip7702>),
}

impl From<MaybeTaggedTxEnvelope> for TxEnvelope {
Expand Down
3 changes: 3 additions & 0 deletions crates/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub use self::optional::*;

pub mod quantity;

/// Serialization for [`alloy_primitves::Sealed`].
pub mod sealed;

/// Storage related helpers.
pub mod storage;
pub use storage::JsonStorageKey;
Expand Down
78 changes: 78 additions & 0 deletions crates/serde/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// Serializes and Deserializes [`Sealed`], flattening the struct.
pub mod flat {
use alloy_primitives::{Sealed, B256};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Serialize)]
struct FlatSer<'a, T> {
seal: B256,
#[serde(flatten)]
t: &'a T,
}

#[derive(Deserialize)]
struct FlatDeser<T> {
seal: B256,
#[serde(flatten)]
t: T,
}

/// Serializes a [`Sealed`] with the given serializer.
pub fn serialize<S, T>(sealed: &Sealed<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
{
FlatSer { seal: sealed.seal(), t: sealed.inner() }.serialize(serializer)
}

/// Deserializes a [`Sealed`] with the given deserializer.
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Sealed<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
let FlatDeser { seal, t } = FlatDeser::deserialize(deserializer)?;
Ok(Sealed::new_unchecked(t, seal))
}
}

/// Serializes and Deserializes [`Sealed`], flattening the struct and renaming
/// the `seal` key to `hash`.
pub mod flat_hash {
use alloy_primitives::{Sealed, B256};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Serialize)]
struct FlatSer<'a, T> {
hash: B256,
#[serde(flatten)]
t: &'a T,
}

#[derive(Deserialize)]
struct FlatDeser<T> {
hash: B256,
#[serde(flatten)]
t: T,
}

/// Serializes a [`Sealed`] with the given serializer.
pub fn serialize<S, T>(sealed: &Sealed<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
{
FlatSer { hash: sealed.seal(), t: sealed.inner() }.serialize(serializer)
}

/// Deserializes a [`Sealed`] with the given deserializer.
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Sealed<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
let FlatDeser { hash, t } = FlatDeser::deserialize(deserializer)?;
Ok(Sealed::new_unchecked(t, hash))
}
}

0 comments on commit 23a5505

Please sign in to comment.