Skip to content

Commit

Permalink
Define trait TransactionEnvelope
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 12, 2025
1 parent 065c123 commit 385ebcd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 73 deletions.
5 changes: 3 additions & 2 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ pub mod transaction;
#[cfg(feature = "kzg")]
pub use transaction::BlobTransactionValidationError;
pub use transaction::{
SignableTransaction, Transaction, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant,
TxEip4844WithSidecar, TxEip7702, TxEnvelope, TxLegacy, TxType, Typed2718, TypedTransaction,
SignableTransaction, Transaction, TransactionEnvelope, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEip7702, TxEnvelope, TxLegacy, TxType, Typed2718,
TypedTransaction,
};

pub use alloy_eips::eip4844::{
Expand Down
131 changes: 61 additions & 70 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ use alloy_primitives::{
use alloy_rlp::{Decodable, Encodable};
use core::fmt;

/// Transaction envelope.
#[auto_impl::auto_impl(&, Arc)]
pub trait TransactionEnvelope: Typed2718 {
/// Returns ref to [`Signed<TxLegacy>`] if transaction is a legacy transaction.
fn as_legacy(&self) -> Option<&Signed<TxLegacy>>;

/// Returns ref to [`Signed<TxEip2930>`] if transaction is an EIP-2930 transaction.
fn as_eip2930(&self) -> Option<&Signed<TxEip2930>>;

/// Returns ref to [`Signed<TxEip1559>`] if transaction is an EIP-1559 transaction.
fn as_eip1559(&self) -> Option<&Signed<TxEip1559>>;

/// Returns ref to [`Signed<TxEip4844>`] if this is an EIP-4844 transaction.
fn as_eip4844(&self) -> Option<&Signed<TxEip4844Variant>>;

/// Returns ref to [`Signed<TxEip7702>`] if this is an EIP-7702 transaction.
fn as_eip7702(&self) -> Option<&Signed<TxEip7702>>;
}

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], [2930],
/// [4844], and [7702].
///
Expand Down Expand Up @@ -224,36 +243,6 @@ impl From<Signed<TxEip7702>> for TxEnvelope {
}

impl TxEnvelope {
/// Returns true if the transaction is a legacy transaction.
#[inline]
pub const fn is_legacy(&self) -> bool {
matches!(self, Self::Legacy(_))
}

/// Returns true if the transaction is an EIP-2930 transaction.
#[inline]
pub const fn is_eip2930(&self) -> bool {
matches!(self, Self::Eip2930(_))
}

/// Returns true if the transaction is an EIP-1559 transaction.
#[inline]
pub const fn is_eip1559(&self) -> bool {
matches!(self, Self::Eip1559(_))
}

/// Returns true if the transaction is an EIP-4844 transaction.
#[inline]
pub const fn is_eip4844(&self) -> bool {
matches!(self, Self::Eip4844(_))
}

/// Returns true if the transaction is an EIP-7702 transaction.
#[inline]
pub const fn is_eip7702(&self) -> bool {
matches!(self, Self::Eip7702(_))
}

/// Returns true if the transaction is replay protected.
///
/// All non-legacy transactions are replay protected, as the chain id is
Expand All @@ -270,46 +259,6 @@ impl TxEnvelope {
}
}

/// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
pub const fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self {
Self::Legacy(tx) => Some(tx),
_ => None,
}
}

/// Returns the [`TxEip2930`] variant if the transaction is an EIP-2930 transaction.
pub const fn as_eip2930(&self) -> Option<&Signed<TxEip2930>> {
match self {
Self::Eip2930(tx) => Some(tx),
_ => None,
}
}

/// Returns the [`TxEip1559`] variant if the transaction is an EIP-1559 transaction.
pub const fn as_eip1559(&self) -> Option<&Signed<TxEip1559>> {
match self {
Self::Eip1559(tx) => Some(tx),
_ => None,
}
}

/// Returns the [`TxEip4844Variant`] variant if the transaction is an EIP-4844 transaction.
pub const fn as_eip4844(&self) -> Option<&Signed<TxEip4844Variant>> {
match self {
Self::Eip4844(tx) => Some(tx),
_ => None,
}
}

/// Returns the [`TxEip7702`] variant if the transaction is an EIP-7702 transaction.
pub const fn as_eip7702(&self) -> Option<&Signed<TxEip7702>> {
match self {
Self::Eip7702(tx) => Some(tx),
_ => None,
}
}

/// Recover the signer of the transaction.
#[cfg(feature = "k256")]
pub fn recover_signer(
Expand Down Expand Up @@ -658,6 +607,48 @@ impl Typed2718 for TxEnvelope {
}
}

impl TransactionEnvelope for TxEnvelope {
#[inline]
fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self {
Self::Legacy(tx) => Some(tx),
_ => None,
}
}

#[inline]
fn as_eip2930(&self) -> Option<&Signed<TxEip2930>> {
match self {
Self::Eip2930(tx) => Some(tx),
_ => None,
}
}

#[inline]
fn as_eip1559(&self) -> Option<&Signed<TxEip1559>> {
match self {
Self::Eip1559(tx) => Some(tx),
_ => None,
}
}

#[inline]
fn as_eip4844(&self) -> Option<&Signed<TxEip4844Variant>> {
match self {
Self::Eip4844(tx) => Some(tx),
_ => None,
}
}

#[inline]
fn as_eip7702(&self) -> Option<&Signed<TxEip7702>> {
match self {
Self::Eip7702(tx) => Some(tx),
_ => None,
}
}
}

#[cfg(feature = "serde")]
mod serde_from {
//! NB: Why do we need this?
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use eip4844::BlobTransactionValidationError;
pub use eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar};

mod envelope;
pub use envelope::{TxEnvelope, TxType};
pub use envelope::{TransactionEnvelope, TxEnvelope, TxType};

mod legacy;
pub use legacy::{from_eip155_value, to_eip155_value, TxLegacy};
Expand Down

0 comments on commit 385ebcd

Please sign in to comment.