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

Define trait TransactionEnvelope #1910

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
61 changes: 61 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,67 @@ impl Typed2718 for TxEnvelope {
}
}

/// 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>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this bakes in the signed type, which I think is fine because I think this will not be used by reth.

but the function name could be problematic, could we introduce as_signed_* instead?

ah I guess this isn't necessary:

pub const fn as_eip2930(&self) -> Option<&Signed<TxEip2930>> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these constant functions are likely not needed anywhere


/// 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>>;
}

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 @@ -36,7 +36,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
Loading