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

fix: allow missing-tag deser of tx envelope #1489

Merged
merged 1 commit into from
Oct 16, 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
81 changes: 75 additions & 6 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ impl TryFrom<u8> for TxType {
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[cfg_attr(
feature = "serde",
serde(into = "serde_from::TaggedTxEnvelope", from = "serde_from::MaybeTaggedTxEnvelope")
)]
#[doc(alias = "TransactionEnvelope")]
#[non_exhaustive]
pub enum TxEnvelope {
/// An untagged [`TxLegacy`].
#[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))]
Legacy(Signed<TxLegacy>),
/// A [`TxEip2930`] tagged with type 1.
#[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))]
Eip2930(Signed<TxEip2930>),
/// A [`TxEip1559`] tagged with type 2.
#[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))]
Eip1559(Signed<TxEip1559>),
/// A TxEip4844 tagged with type 3.
/// An EIP-4844 transaction has two network representations:
Expand All @@ -107,10 +107,8 @@ pub enum TxEnvelope {
///
/// 2 - The transaction with a sidecar, which is the form used to
/// send transactions to the network.
#[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))]
Eip4844(Signed<TxEip4844Variant>),
/// A [`TxEip7702`] tagged with type 4.
#[cfg_attr(feature = "serde", serde(rename = "0x4", alias = "0x04"))]
Eip7702(Signed<TxEip7702>),
}

Expand Down Expand Up @@ -518,6 +516,77 @@ impl Transaction for TxEnvelope {
}
}

#[cfg(feature = "serde")]
mod serde_from {
//! NB: Why do we need this?
//!
//! Because the tag may be missing, we need an abstraction over tagged (with
//! type) and untagged (always legacy). This is [`MaybeTaggedTxEnvelope`].
//!
//! The tagged variant is [`TaggedTxEnvelope`], which always has a type tag.
//!
//! We serialize via [`TaggedTxEnvelope`] and deserialize via
//! [`MaybeTaggedTxEnvelope`].
use crate::{Signed, TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702, TxLegacy};

use super::TxEnvelope;

#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub(crate) enum MaybeTaggedTxEnvelope {
Tagged(TaggedTxEnvelope),
Untagged(Signed<TxLegacy>),
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type")]
pub(crate) enum TaggedTxEnvelope {
#[serde(rename = "0x0", alias = "0x00")]
Legacy(Signed<TxLegacy>),
#[serde(rename = "0x1", alias = "0x01")]
Eip2930(Signed<TxEip2930>),
#[serde(rename = "0x2", alias = "0x02")]
Eip1559(Signed<TxEip1559>),
#[serde(rename = "0x3", alias = "0x03")]
Eip4844(Signed<TxEip4844Variant>),
#[serde(rename = "0x4", alias = "0x04")]
Eip7702(Signed<TxEip7702>),
}

impl From<MaybeTaggedTxEnvelope> for TxEnvelope {
fn from(value: MaybeTaggedTxEnvelope) -> Self {
match value {
MaybeTaggedTxEnvelope::Tagged(tagged) => tagged.into(),
MaybeTaggedTxEnvelope::Untagged(tx) => Self::Legacy(tx),
}
}
}

impl From<TaggedTxEnvelope> for TxEnvelope {
fn from(value: TaggedTxEnvelope) -> Self {
match value {
TaggedTxEnvelope::Legacy(signed) => Self::Legacy(signed),
TaggedTxEnvelope::Eip2930(signed) => Self::Eip2930(signed),
TaggedTxEnvelope::Eip1559(signed) => Self::Eip1559(signed),
TaggedTxEnvelope::Eip4844(signed) => Self::Eip4844(signed),
TaggedTxEnvelope::Eip7702(signed) => Self::Eip7702(signed),
}
}
}

impl From<TxEnvelope> for TaggedTxEnvelope {
fn from(value: TxEnvelope) -> Self {
match value {
TxEnvelope::Legacy(signed) => Self::Legacy(signed),
TxEnvelope::Eip2930(signed) => Self::Eip2930(signed),
TxEnvelope::Eip1559(signed) => Self::Eip1559(signed),
TxEnvelope::Eip4844(signed) => Self::Eip4844(signed),
TxEnvelope::Eip7702(signed) => Self::Eip7702(signed),
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down