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 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
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, TypedTransaction,
SignableTransaction, Transaction, TransactionEnvelope, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEip7702, TxEnvelope, TxLegacy, TxType,
TypedTransaction,
};

pub use alloy_eips::{
Expand Down
101 changes: 61 additions & 40 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,46 +271,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 @@ -649,6 +609,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 @@ -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
86 changes: 44 additions & 42 deletions crates/network/src/any/either.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{UnknownTxEnvelope, UnknownTypedTransaction};
use alloy_consensus::{
Signed, Transaction as TransactionTrait, TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702,
TxEnvelope, TxLegacy, Typed2718, TypedTransaction,
Signed, Transaction as TransactionTrait, TransactionEnvelope, TxEip1559, TxEip2930,
TxEip4844Variant, TxEip7702, TxEnvelope, TxLegacy, Typed2718, TypedTransaction,
};
use alloy_eips::{
eip2718::{Decodable2718, Encodable2718},
Expand Down Expand Up @@ -231,46 +231,6 @@ impl AnyTxEnvelope {
this => Err(this),
}
}

/// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
pub const fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self.as_envelope() {
Some(TxEnvelope::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.as_envelope() {
Some(TxEnvelope::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.as_envelope() {
Some(TxEnvelope::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.as_envelope() {
Some(TxEnvelope::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.as_envelope() {
Some(TxEnvelope::Eip7702(tx)) => Some(tx),
_ => None,
}
}
}

impl Typed2718 for AnyTxEnvelope {
Expand Down Expand Up @@ -453,3 +413,45 @@ impl TransactionTrait for AnyTxEnvelope {
}
}
}

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

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

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

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

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