From 6dc00a29c998b5e3186bb1ffc8261dc5b7dc25b4 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Sat, 24 Aug 2024 06:32:10 -0700 Subject: [PATCH] tx-pool: rm into tx constraint for `PoolTransaction` (#10057) Co-authored-by: Arsenii Kulikov --- crates/net/network/src/transactions/mod.rs | 10 ++++-- crates/optimism/node/src/txpool.rs | 2 +- .../rpc-eth-api/src/helpers/transaction.rs | 4 ++- crates/rpc/rpc/src/eth/filter.rs | 9 ++++-- crates/rpc/rpc/src/txpool.rs | 8 ++--- crates/transaction-pool/src/ordering.rs | 12 +++++-- .../transaction-pool/src/test_utils/mock.rs | 4 +++ crates/transaction-pool/src/traits.rs | 32 ++++++++++++------- crates/transaction-pool/src/validate/mod.rs | 13 +++++--- 9 files changed, 63 insertions(+), 31 deletions(-) diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index 9e6bd4efc..79817dd3c 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -47,7 +47,9 @@ use reth_network_p2p::{ }; use reth_network_peers::PeerId; use reth_network_types::ReputationChangeKind; -use reth_primitives::{PooledTransactionsElement, TransactionSigned, TxHash, B256}; +use reth_primitives::{ + PooledTransactionsElement, TransactionSigned, TransactionSignedEcRecovered, TxHash, B256, +}; use reth_tokio_util::EventStream; use reth_transaction_pool::{ error::{PoolError, PoolResult}, @@ -1395,9 +1397,11 @@ impl PropagateTransaction { } /// Create a new instance from a pooled transaction - fn new(tx: Arc>) -> Self { + fn new>( + tx: Arc>, + ) -> Self { let size = tx.encoded_length(); - let transaction = Arc::new(tx.transaction.clone().into().into_signed()); + let transaction = Arc::new(tx.transaction.clone().into_consensus().into_signed()); Self { size, transaction } } } diff --git a/crates/optimism/node/src/txpool.rs b/crates/optimism/node/src/txpool.rs index 6f9e64750..bbe9ccef3 100644 --- a/crates/optimism/node/src/txpool.rs +++ b/crates/optimism/node/src/txpool.rs @@ -134,7 +134,7 @@ where let l1_block_info = self.block_info.l1_block_info.read().clone(); let mut encoded = Vec::with_capacity(valid_tx.transaction().encoded_length()); - valid_tx.transaction().clone().into().encode_enveloped(&mut encoded); + valid_tx.transaction().clone().into_consensus().encode_enveloped(&mut encoded); let cost_addition = match l1_block_info.l1_tx_data_fee( &self.chain_spec(), diff --git a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs index 124da5f31..65efea374 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs @@ -607,7 +607,9 @@ pub trait LoadTransaction: SpawnBlocking { if resp.is_none() { // tx not found on disk, check pool - if let Some(tx) = self.pool().get(&hash).map(|tx| tx.transaction.clone().into()) { + if let Some(tx) = + self.pool().get(&hash).map(|tx| tx.transaction.clone().into_consensus()) + { resp = Some(TransactionSource::Pool(tx)); } } diff --git a/crates/rpc/rpc/src/eth/filter.rs b/crates/rpc/rpc/src/eth/filter.rs index a809f76ae..9645bb6ec 100644 --- a/crates/rpc/rpc/src/eth/filter.rs +++ b/crates/rpc/rpc/src/eth/filter.rs @@ -12,7 +12,7 @@ use std::{ use async_trait::async_trait; use jsonrpsee::{core::RpcResult, server::IdProvider}; use reth_chainspec::ChainInfo; -use reth_primitives::{IntoRecoveredTransaction, TxHash}; +use reth_primitives::{IntoRecoveredTransaction, TransactionSignedEcRecovered, TxHash}; use reth_provider::{BlockIdReader, BlockReader, EvmEnvProvider, ProviderError}; use reth_rpc_eth_api::EthFilterApiServer; use reth_rpc_eth_types::{ @@ -573,7 +573,10 @@ where } /// Returns all new pending transactions received since the last poll. - async fn drain(&self) -> FilterChanges { + async fn drain(&self) -> FilterChanges + where + T: PoolTransaction, + { let mut pending_txs = Vec::new(); let mut prepared_stream = self.txs_stream.lock().await; @@ -595,7 +598,7 @@ trait FullTransactionsFilter: fmt::Debug + Send + Sync + Unpin + 'static { #[async_trait] impl FullTransactionsFilter for FullTransactionsReceiver where - T: PoolTransaction + 'static, + T: PoolTransaction + 'static, { async fn drain(&self) -> FilterChanges { Self::drain(self).await diff --git a/crates/rpc/rpc/src/txpool.rs b/crates/rpc/rpc/src/txpool.rs index 682a4558d..95a21014f 100644 --- a/crates/rpc/rpc/src/txpool.rs +++ b/crates/rpc/rpc/src/txpool.rs @@ -32,13 +32,13 @@ where { fn content(&self) -> TxpoolContent { #[inline] - fn insert( + fn insert>( tx: &T, content: &mut BTreeMap>, ) { content.entry(tx.sender()).or_default().insert( tx.nonce().to_string(), - reth_rpc_types_compat::transaction::from_recovered(tx.clone().into()), + reth_rpc_types_compat::transaction::from_recovered(tx.clone().into_consensus()), ); } @@ -82,12 +82,12 @@ where trace!(target: "rpc::eth", "Serving txpool_inspect"); #[inline] - fn insert( + fn insert>( tx: &T, inspect: &mut BTreeMap>, ) { let entry = inspect.entry(tx.sender()).or_default(); - let tx: TransactionSignedEcRecovered = tx.clone().into(); + let tx = tx.clone().into_consensus(); entry.insert( tx.nonce().to_string(), TxpoolInspectSummary { diff --git a/crates/transaction-pool/src/ordering.rs b/crates/transaction-pool/src/ordering.rs index 73b8db9f6..15b5accd9 100644 --- a/crates/transaction-pool/src/ordering.rs +++ b/crates/transaction-pool/src/ordering.rs @@ -1,5 +1,5 @@ use crate::traits::PoolTransaction; -use reth_primitives::{PooledTransactionsElementEcRecovered, U256}; +use reth_primitives::{PooledTransactionsElementEcRecovered, TransactionSignedEcRecovered, U256}; use std::{fmt, marker::PhantomData}; /// Priority of the transaction that can be missing. @@ -31,7 +31,10 @@ pub trait TransactionOrdering: Send + Sync + 'static { type PriorityValue: Ord + Clone + Default + fmt::Debug + Send + Sync; /// The transaction type to determine the priority of. - type Transaction: PoolTransaction; + type Transaction: PoolTransaction< + Pooled = PooledTransactionsElementEcRecovered, + Consensus = TransactionSignedEcRecovered, + >; /// Returns the priority score for the given transaction. fn priority( @@ -51,7 +54,10 @@ pub struct CoinbaseTipOrdering(PhantomData); impl TransactionOrdering for CoinbaseTipOrdering where - T: PoolTransaction + 'static, + T: PoolTransaction< + Pooled = PooledTransactionsElementEcRecovered, + Consensus = TransactionSignedEcRecovered, + > + 'static, { type PriorityValue = U256; type Transaction = T; diff --git a/crates/transaction-pool/src/test_utils/mock.rs b/crates/transaction-pool/src/test_utils/mock.rs index 339020215..90e260e1c 100644 --- a/crates/transaction-pool/src/test_utils/mock.rs +++ b/crates/transaction-pool/src/test_utils/mock.rs @@ -562,6 +562,10 @@ impl PoolTransaction for MockTransaction { type Pooled = PooledTransactionsElementEcRecovered; + fn into_consensus(self) -> Self::Consensus { + self.into() + } + fn from_pooled(pooled: Self::Pooled) -> Self { pooled.into() } diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 1a627e8e1..6c79618f1 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -42,7 +42,10 @@ pub type PeerId = reth_primitives::B512; #[auto_impl::auto_impl(&, Arc)] pub trait TransactionPool: Send + Sync + Clone { /// The transaction type of the pool - type Transaction: PoolTransaction; + type Transaction: PoolTransaction< + Pooled = PooledTransactionsElementEcRecovered, + Consensus = TransactionSignedEcRecovered, + >; /// Returns stats about the pool and all sub-pools. fn pool_size(&self) -> PoolSize; @@ -484,13 +487,13 @@ pub struct AllPoolTransactions { impl AllPoolTransactions { /// Returns an iterator over all pending [`TransactionSignedEcRecovered`] transactions. - pub fn pending_recovered(&self) -> impl Iterator + '_ { - self.pending.iter().map(|tx| tx.transaction.clone().into()) + pub fn pending_recovered(&self) -> impl Iterator + '_ { + self.pending.iter().map(|tx| tx.transaction.clone().into_consensus()) } /// Returns an iterator over all queued [`TransactionSignedEcRecovered`] transactions. - pub fn queued_recovered(&self) -> impl Iterator + '_ { - self.queued.iter().map(|tx| tx.transaction.clone().into()) + pub fn queued_recovered(&self) -> impl Iterator + '_ { + self.queued.iter().map(|tx| tx.transaction.clone().into_consensus()) } } @@ -771,12 +774,7 @@ impl BestTransactionsAttributes { /// Trait for transaction types used inside the pool pub trait PoolTransaction: - fmt::Debug - + Send - + Sync - + Clone - + TryFrom - + Into + fmt::Debug + Send + Sync + Clone + TryFrom { /// Associated type representing the raw consensus variant of the transaction. type Consensus: From + TryInto; @@ -784,6 +782,9 @@ pub trait PoolTransaction: /// Associated type representing the recovered pooled variant of the transaction. type Pooled: Into; + /// Define a method to convert from the `Self` type to `Consensus` + fn into_consensus(self) -> Self::Consensus; + /// Define a method to convert from the `Pooled` type to `Self` fn from_pooled(pooled: Self::Pooled) -> Self; @@ -884,7 +885,10 @@ pub trait PoolTransaction: /// An extension trait that provides additional interfaces for the /// [`EthTransactionValidator`](crate::EthTransactionValidator). pub trait EthPoolTransaction: - PoolTransaction + PoolTransaction< + Pooled = PooledTransactionsElementEcRecovered, + Consensus = TransactionSignedEcRecovered, +> { /// Extracts the blob sidecar from the transaction. fn take_blob(&mut self) -> EthBlobTransactionSidecar; @@ -1024,6 +1028,10 @@ impl PoolTransaction for EthPooledTransaction { type Pooled = PooledTransactionsElementEcRecovered; + fn into_consensus(self) -> Self::Consensus { + self.into() + } + fn from_pooled(pooled: Self::Pooled) -> Self { pooled.into() } diff --git a/crates/transaction-pool/src/validate/mod.rs b/crates/transaction-pool/src/validate/mod.rs index 1645af524..c1f6066a0 100644 --- a/crates/transaction-pool/src/validate/mod.rs +++ b/crates/transaction-pool/src/validate/mod.rs @@ -154,7 +154,10 @@ impl ValidTransaction { /// Provides support for validating transaction at any given state of the chain pub trait TransactionValidator: Send + Sync { /// The transaction type to validate. - type Transaction: PoolTransaction; + type Transaction: PoolTransaction< + Pooled = PooledTransactionsElementEcRecovered, + Consensus = TransactionSignedEcRecovered, + >; /// Validates the transaction and returns a [`TransactionValidationOutcome`] describing the /// validity of the given transaction. @@ -377,14 +380,16 @@ impl ValidPoolTransaction { } } -impl IntoRecoveredTransaction for ValidPoolTransaction { +impl> IntoRecoveredTransaction + for ValidPoolTransaction +{ fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { - self.transaction.clone().into() + self.transaction.clone().into_consensus() } } #[cfg(test)] -impl Clone for ValidPoolTransaction { +impl Clone for ValidPoolTransaction { fn clone(&self) -> Self { Self { transaction: self.transaction.clone(),