Skip to content

refactor: move ExtendedTxEnvelope to reth-primitives-traits #16102

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

Merged
merged 7 commits into from
May 8, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
use alloy_consensus::{error::ValueError, Transaction};
use crate::{
size::InMemorySize,
transaction::signed::{RecoveryError, SignedTransaction},
};
use alloc::vec::Vec;
use alloy_consensus::Transaction;
use alloy_eips::{
eip2718::{Eip2718Error, Eip2718Result, IsTyped2718},
eip2930::AccessList,
eip7702::SignedAuthorization,
Decodable2718, Encodable2718, Typed2718,
};
use alloy_primitives::{bytes::Buf, ChainId, Signature, TxHash};
use alloy_primitives::{ChainId, TxHash};
use alloy_rlp::{BufMut, Decodable, Encodable, Result as RlpResult};
use op_alloy_consensus::{OpPooledTransaction, OpTxEnvelope};
use reth_codecs::Compact;
use reth_ethereum::primitives::{
serde_bincode_compat::SerdeBincodeCompat, transaction::signed::RecoveryError, InMemorySize,
SignedTransaction,
};
use revm_primitives::{Address, Bytes, TxKind, B256, U256};

use super::CustomTransactionEnvelope;
#[cfg(feature = "op")]
use op_alloy_consensus::{OpPooledTransaction, OpTxEnvelope};

#[cfg(feature = "op")]
use alloy_primitives::Signature;

#[cfg(feature = "op")]
use alloy_consensus::error::ValueError;

macro_rules! delegate {
($self:expr => $tx:ident.$method:ident($($arg:expr),*)) => {
Expand All @@ -33,44 +39,44 @@ macro_rules! delegate {
///
/// Note: The other transaction type variants must not overlap with the builtin one, transaction
/// types must be unique.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum ExtendedTxEnvelope<BuiltIn, Other> {
/// The builtin transaction type.
BuiltIn(BuiltIn),
/// The other transaction type.
Other(Other),
}

pub type ExtendedOpTxEnvelope<T> = ExtendedTxEnvelope<OpTxEnvelope, T>;

impl TryFrom<ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope>>
for ExtendedTxEnvelope<OpPooledTransaction, CustomTransactionEnvelope>
#[cfg(feature = "op")]
impl<Tx> TryFrom<ExtendedTxEnvelope<OpTxEnvelope, Tx>>
for ExtendedTxEnvelope<OpPooledTransaction, Tx>
{
Comment on lines +52 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

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

we either need to relax this so that this works for any Builtin type or feature gat behind op feature

type Error = OpTxEnvelope;

fn try_from(
value: ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope>,
) -> Result<Self, Self::Error> {
fn try_from(value: ExtendedTxEnvelope<OpTxEnvelope, Tx>) -> Result<Self, Self::Error> {
match value {
ExtendedTxEnvelope::BuiltIn(tx) => {
let converted_tx: OpPooledTransaction = tx.clone().try_into().map_err(|_| tx)?;
Ok(ExtendedTxEnvelope::BuiltIn(converted_tx))
Ok(Self::BuiltIn(converted_tx))
}
ExtendedTxEnvelope::Other(tx) => Ok(ExtendedTxEnvelope::Other(tx)),
ExtendedTxEnvelope::Other(tx) => Ok(Self::Other(tx)),
}
}
}

impl From<OpPooledTransaction> for ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope> {
#[cfg(feature = "op")]
impl<Tx> From<OpPooledTransaction> for ExtendedTxEnvelope<OpTxEnvelope, Tx> {
fn from(tx: OpPooledTransaction) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this also needs an op feature

ExtendedTxEnvelope::BuiltIn(tx.into())
Self::BuiltIn(tx.into())
}
}

impl TryFrom<ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope>> for OpPooledTransaction {
#[cfg(feature = "op")]
impl<Tx> TryFrom<ExtendedTxEnvelope<OpTxEnvelope, Tx>> for OpPooledTransaction {
type Error = ValueError<OpTxEnvelope>;

fn try_from(
_tx: ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope>,
) -> Result<Self, Self::Error> {
fn try_from(_tx: ExtendedTxEnvelope<OpTxEnvelope, Tx>) -> Result<Self, Self::Error> {
match _tx {
ExtendedTxEnvelope::BuiltIn(inner) => inner.try_into(),
ExtendedTxEnvelope::Other(_tx) => Err(ValueError::new(
Expand Down Expand Up @@ -303,38 +309,49 @@ where
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum ExtendedTxEnvelopeRepr<'a, B: SerdeBincodeCompat, T: SerdeBincodeCompat> {
BuiltIn(B::BincodeRepr<'a>),
Other(T::BincodeRepr<'a>),
}
#[cfg(feature = "serde-bincode-compat")]
mod serde_bincode_compat {
use super::*;
use crate::serde_bincode_compat::SerdeBincodeCompat;

impl<B, T> SerdeBincodeCompat for ExtendedTxEnvelope<B, T>
where
B: SerdeBincodeCompat + std::fmt::Debug,
T: SerdeBincodeCompat + std::fmt::Debug,
{
type BincodeRepr<'a> = ExtendedTxEnvelopeRepr<'a, B, T>;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug)]
pub enum ExtendedTxEnvelopeRepr<'a, B: SerdeBincodeCompat, T: SerdeBincodeCompat> {
BuiltIn(B::BincodeRepr<'a>),
Other(T::BincodeRepr<'a>),
}

fn as_repr(&self) -> Self::BincodeRepr<'_> {
match self {
Self::BuiltIn(tx) => ExtendedTxEnvelopeRepr::BuiltIn(tx.as_repr()),
Self::Other(tx) => ExtendedTxEnvelopeRepr::Other(tx.as_repr()),
impl<B, T> SerdeBincodeCompat for ExtendedTxEnvelope<B, T>
where
B: SerdeBincodeCompat + core::fmt::Debug,
T: SerdeBincodeCompat + core::fmt::Debug,
{
type BincodeRepr<'a> = ExtendedTxEnvelopeRepr<'a, B, T>;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
match self {
Self::BuiltIn(tx) => ExtendedTxEnvelopeRepr::BuiltIn(tx.as_repr()),
Self::Other(tx) => ExtendedTxEnvelopeRepr::Other(tx.as_repr()),
}
}
}

fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
match repr {
ExtendedTxEnvelopeRepr::BuiltIn(tx_repr) => Self::BuiltIn(B::from_repr(tx_repr)),
ExtendedTxEnvelopeRepr::Other(tx_repr) => Self::Other(T::from_repr(tx_repr)),
fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
match repr {
ExtendedTxEnvelopeRepr::BuiltIn(tx_repr) => Self::BuiltIn(B::from_repr(tx_repr)),
ExtendedTxEnvelopeRepr::Other(tx_repr) => Self::Other(T::from_repr(tx_repr)),
}
}
}
}

impl<B, T> Compact for ExtendedTxEnvelope<B, T>
#[cfg(feature = "reth-codec")]
use alloy_primitives::bytes::Buf;

#[cfg(feature = "reth-codec")]
impl<B, T> reth_codecs::Compact for ExtendedTxEnvelope<B, T>
where
B: Transaction + IsTyped2718 + Compact,
T: Transaction + Compact,
B: Transaction + IsTyped2718 + reth_codecs::Compact,
T: Transaction + reth_codecs::Compact,
{
fn to_compact<Buf>(&self, buf: &mut Buf) -> usize
where
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub use storage::StorageEntry;

pub mod sync;

mod extended;
pub use extended::ExtendedTxEnvelope;
/// Common header types
pub mod header;
pub use header::{Header, HeaderError, SealedHeader, SealedHeaderFor};
Expand Down
3 changes: 1 addition & 2 deletions examples/custom-node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
chainspec::CustomChainSpec,
primitives::{
CustomHeader, CustomNodePrimitives, CustomTransactionEnvelope, ExtendedOpTxEnvelope,
ExtendedTxEnvelope,
},
};
use alloy_consensus::{Block, BlockBody};
Expand All @@ -15,7 +14,7 @@ use reth_ethereum::{
pool::{PoolTransaction, TransactionPool},
};
use reth_node_builder::{components::NetworkBuilder, BuilderContext};
use reth_op::OpReceipt;
use reth_op::{primitives::ExtendedTxEnvelope, OpReceipt};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-node/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// use jsonrpsee::tracing::{debug, info};
use crate::primitives::CustomTransactionEnvelope;
use op_alloy_consensus::{interop::SafetyLevel, OpTxEnvelope};
use reth_chain_state::CanonStateSubscriptions;
use reth_node_builder::{
Expand All @@ -17,11 +18,10 @@ use reth_op::{
blobstore::DiskFileBlobStore, CoinbaseTipOrdering, EthPoolTransaction,
TransactionValidationTaskExecutor,
},
primitives::ExtendedTxEnvelope,
};
use reth_optimism_forks::OpHardforks;

use crate::primitives::{CustomTransactionEnvelope, ExtendedTxEnvelope};

#[derive(Debug, Clone)]
pub struct CustomPoolBuilder<
T = OpPooledTransaction<ExtendedTxEnvelope<OpTxEnvelope, CustomTransactionEnvelope>>,
Expand Down
4 changes: 1 addition & 3 deletions examples/custom-node/src/primitives/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::primitives::CustomHeader;

use super::{CustomTransactionEnvelope, ExtendedOpTxEnvelope};
use crate::primitives::{CustomHeader, CustomTransactionEnvelope, ExtendedOpTxEnvelope};

/// The Block type of this node
pub type Block =
Expand Down
2 changes: 0 additions & 2 deletions examples/custom-node/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub use tx::*;

pub mod tx_type;
pub use tx_type::*;
pub mod extended_op_tx_envelope;
pub use extended_op_tx_envelope::*;
pub mod tx_custom;
pub use tx_custom::*;

Expand Down
7 changes: 6 additions & 1 deletion examples/custom-node/src/primitives/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ use alloy_consensus::{
use alloy_eips::{eip2718::Eip2718Result, Decodable2718, Encodable2718, Typed2718};
use alloy_primitives::{keccak256, Signature, TxHash};
use alloy_rlp::{BufMut, Decodable, Encodable, Result as RlpResult};
use op_alloy_consensus::OpTxEnvelope;
use reth_codecs::{
alloy::transaction::{FromTxCompact, ToTxCompact},
Compact,
};
use reth_ethereum::primitives::{serde_bincode_compat::SerdeBincodeCompat, InMemorySize};
use reth_op::primitives::SignedTransaction;
use reth_op::primitives::{ExtendedTxEnvelope, SignedTransaction};
use revm_primitives::{Address, Bytes};
use serde::{Deserialize, Serialize};

/// A [`SignedTransaction`] implementation that combines the [`OpTxEnvelope`] and another
/// transaction type.
pub type ExtendedOpTxEnvelope<T> = ExtendedTxEnvelope<OpTxEnvelope, T>;

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct CustomTransactionEnvelope {
pub inner: Signed<TxCustom>,
Expand Down
Loading