From 124bd39405fe5c802dec8bbf6cc5cf34a20d81f1 Mon Sep 17 00:00:00 2001 From: Femi Bankole Date: Thu, 8 May 2025 17:32:54 +0100 Subject: [PATCH] refactor: move ExtendedTxEnvelope to reth-primitives-traits (#16102) Co-authored-by: Federico Gimenez --- .../primitives-traits/src/extended.rs | 115 ++++++++++-------- crates/primitives-traits/src/lib.rs | 2 + examples/custom-node/src/network.rs | 3 +- examples/custom-node/src/pool.rs | 4 +- examples/custom-node/src/primitives/block.rs | 4 +- examples/custom-node/src/primitives/mod.rs | 2 - examples/custom-node/src/primitives/tx.rs | 7 +- 7 files changed, 78 insertions(+), 59 deletions(-) rename examples/custom-node/src/primitives/extended_op_tx_envelope.rs => crates/primitives-traits/src/extended.rs (75%) diff --git a/examples/custom-node/src/primitives/extended_op_tx_envelope.rs b/crates/primitives-traits/src/extended.rs similarity index 75% rename from examples/custom-node/src/primitives/extended_op_tx_envelope.rs rename to crates/primitives-traits/src/extended.rs index 2a48027f66..4986485fa7 100644 --- a/examples/custom-node/src/primitives/extended_op_tx_envelope.rs +++ b/crates/primitives-traits/src/extended.rs @@ -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),*)) => { @@ -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 { + /// The builtin transaction type. BuiltIn(BuiltIn), + /// The other transaction type. Other(Other), } -pub type ExtendedOpTxEnvelope = ExtendedTxEnvelope; - -impl TryFrom> - for ExtendedTxEnvelope +#[cfg(feature = "op")] +impl TryFrom> + for ExtendedTxEnvelope { type Error = OpTxEnvelope; - fn try_from( - value: ExtendedTxEnvelope, - ) -> Result { + fn try_from(value: ExtendedTxEnvelope) -> Result { 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 for ExtendedTxEnvelope { +#[cfg(feature = "op")] +impl From for ExtendedTxEnvelope { fn from(tx: OpPooledTransaction) -> Self { - ExtendedTxEnvelope::BuiltIn(tx.into()) + Self::BuiltIn(tx.into()) } } -impl TryFrom> for OpPooledTransaction { +#[cfg(feature = "op")] +impl TryFrom> for OpPooledTransaction { type Error = ValueError; - fn try_from( - _tx: ExtendedTxEnvelope, - ) -> Result { + fn try_from(_tx: ExtendedTxEnvelope) -> Result { match _tx { ExtendedTxEnvelope::BuiltIn(inner) => inner.try_into(), ExtendedTxEnvelope::Other(_tx) => Err(ValueError::new( @@ -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 SerdeBincodeCompat for ExtendedTxEnvelope -where - B: SerdeBincodeCompat + std::fmt::Debug, - T: SerdeBincodeCompat + std::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()), - } + #[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 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 SerdeBincodeCompat for ExtendedTxEnvelope + 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)), + } } } } -impl Compact for ExtendedTxEnvelope +#[cfg(feature = "reth-codec")] +use alloy_primitives::bytes::Buf; + +#[cfg(feature = "reth-codec")] +impl reth_codecs::Compact for ExtendedTxEnvelope where - B: Transaction + IsTyped2718 + Compact, - T: Transaction + Compact, + B: Transaction + IsTyped2718 + reth_codecs::Compact, + T: Transaction + reth_codecs::Compact, { fn to_compact(&self, buf: &mut Buf) -> usize where diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index eb64a1d78b..4a6d58ab8d 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -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}; diff --git a/examples/custom-node/src/network.rs b/examples/custom-node/src/network.rs index 38dd12b2e5..0a58c338aa 100644 --- a/examples/custom-node/src/network.rs +++ b/examples/custom-node/src/network.rs @@ -2,7 +2,6 @@ use crate::{ chainspec::CustomChainSpec, primitives::{ CustomHeader, CustomNodePrimitives, CustomTransactionEnvelope, ExtendedOpTxEnvelope, - ExtendedTxEnvelope, }, }; use alloy_consensus::{Block, BlockBody}; @@ -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] diff --git a/examples/custom-node/src/pool.rs b/examples/custom-node/src/pool.rs index 4baedb28c2..5b024089d3 100644 --- a/examples/custom-node/src/pool.rs +++ b/examples/custom-node/src/pool.rs @@ -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::{ @@ -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>, diff --git a/examples/custom-node/src/primitives/block.rs b/examples/custom-node/src/primitives/block.rs index 1415c72b13..3de2831c41 100644 --- a/examples/custom-node/src/primitives/block.rs +++ b/examples/custom-node/src/primitives/block.rs @@ -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 = diff --git a/examples/custom-node/src/primitives/mod.rs b/examples/custom-node/src/primitives/mod.rs index bba93b53d3..dd9a2228a2 100644 --- a/examples/custom-node/src/primitives/mod.rs +++ b/examples/custom-node/src/primitives/mod.rs @@ -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::*; diff --git a/examples/custom-node/src/primitives/tx.rs b/examples/custom-node/src/primitives/tx.rs index f64451a3ee..d862281bf6 100644 --- a/examples/custom-node/src/primitives/tx.rs +++ b/examples/custom-node/src/primitives/tx.rs @@ -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 = ExtendedTxEnvelope; + #[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)] pub struct CustomTransactionEnvelope { pub inner: Signed,