From 054f8ffbb3e3680c21eab6b2efcb6d63872b63d4 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:45:07 +0200 Subject: [PATCH] tx-pool: rm constraint for `PoolTransaction` (#10024) --- crates/net/network/src/transactions/mod.rs | 2 +- crates/rpc/rpc-eth-api/src/helpers/transaction.rs | 3 ++- crates/transaction-pool/src/maintain.rs | 8 +++++--- crates/transaction-pool/src/ordering.rs | 6 +++--- crates/transaction-pool/src/test_utils/mock.rs | 4 ++++ crates/transaction-pool/src/traits.rs | 14 +++++++++++--- crates/transaction-pool/src/validate/mod.rs | 7 ++++--- 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index 7e9c52f5fd..adda3557d6 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -1004,7 +1004,7 @@ where Entry::Vacant(entry) => { if !self.bad_imports.contains(tx.hash()) { // this is a new transaction that should be imported into the pool - let pool_transaction: Pool::Transaction = tx.into(); + let pool_transaction = Pool::Transaction::from_pooled(tx); new_txs.push(pool_transaction); entry.insert(HashSet::from([peer_id])); diff --git a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs index 09d212348b..ec0048e3ee 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs @@ -250,7 +250,8 @@ pub trait EthTransactions: LoadTransaction { ) -> impl Future> + Send { async move { let recovered = recover_raw_transaction(tx.clone())?; - let pool_transaction: ::Transaction = recovered.into(); + let pool_transaction = + ::Transaction::from_pooled(recovered); // On optimism, transactions are forwarded directly to the sequencer to be included in // blocks that it builds. diff --git a/crates/transaction-pool/src/maintain.rs b/crates/transaction-pool/src/maintain.rs index f5b03dfab6..9470f4542c 100644 --- a/crates/transaction-pool/src/maintain.rs +++ b/crates/transaction-pool/src/maintain.rs @@ -5,7 +5,7 @@ use crate::{ error::PoolError, metrics::MaintainPoolMetrics, traits::{CanonicalStateUpdate, ChangedAccount, TransactionPool, TransactionPoolExt}, - BlockInfo, + BlockInfo, PoolTransaction, }; use futures_util::{ future::{BoxFuture, Fuse, FusedFuture}, @@ -331,7 +331,9 @@ pub async fn maintain_transaction_pool( ) .ok() }) - .map(Into::into) + .map(|tx| { + <

::Transaction as PoolTransaction>::from_pooled(tx) + }) } else { tx.try_into().ok() } @@ -674,7 +676,7 @@ mod tests { use super::*; use crate::{ blobstore::InMemoryBlobStore, validate::EthTransactionValidatorBuilder, - CoinbaseTipOrdering, EthPooledTransaction, Pool, PoolTransaction, TransactionOrigin, + CoinbaseTipOrdering, EthPooledTransaction, Pool, TransactionOrigin, }; use reth_chainspec::MAINNET; use reth_fs_util as fs; diff --git a/crates/transaction-pool/src/ordering.rs b/crates/transaction-pool/src/ordering.rs index e4928710b6..73b8db9f66 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::U256; +use reth_primitives::{PooledTransactionsElementEcRecovered, U256}; use std::{fmt, marker::PhantomData}; /// Priority of the transaction that can be missing. @@ -31,7 +31,7 @@ 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; /// Returns the priority score for the given transaction. fn priority( @@ -51,7 +51,7 @@ pub struct CoinbaseTipOrdering(PhantomData); impl TransactionOrdering for CoinbaseTipOrdering where - T: PoolTransaction + 'static, + T: PoolTransaction + '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 ae2e7fa2bd..6687c3e344 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 from_pooled(pooled: Self::Pooled) -> Self { + pooled.into() + } + fn hash(&self) -> &TxHash { match self { Self::Legacy { hash, .. } | diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index b2337c73b3..371f7097fe 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -41,7 +41,7 @@ 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; /// Returns stats about the pool and all sub-pools. fn pool_size(&self) -> PoolSize; @@ -759,7 +759,6 @@ pub trait PoolTransaction: + Send + Sync + Clone - + From + TryFrom + Into { @@ -769,6 +768,9 @@ pub trait PoolTransaction: /// Associated type representing the recovered pooled variant of the transaction. type Pooled: Into; + /// Define a method to convert from the `Pooled` type to `Self` + fn from_pooled(pooled: Self::Pooled) -> Self; + /// Hash of the transaction. fn hash(&self) -> &TxHash; @@ -865,7 +867,9 @@ pub trait PoolTransaction: /// An extension trait that provides additional interfaces for the /// [`EthTransactionValidator`](crate::EthTransactionValidator). -pub trait EthPoolTransaction: PoolTransaction { +pub trait EthPoolTransaction: + PoolTransaction +{ /// Extracts the blob sidecar from the transaction. fn take_blob(&mut self) -> EthBlobTransactionSidecar; @@ -1004,6 +1008,10 @@ impl PoolTransaction for EthPooledTransaction { type Pooled = PooledTransactionsElementEcRecovered; + fn from_pooled(pooled: Self::Pooled) -> Self { + pooled.into() + } + /// Returns hash of the transaction. fn hash(&self) -> &TxHash { self.transaction.hash_ref() diff --git a/crates/transaction-pool/src/validate/mod.rs b/crates/transaction-pool/src/validate/mod.rs index df75783ab6..5c4a2f8314 100644 --- a/crates/transaction-pool/src/validate/mod.rs +++ b/crates/transaction-pool/src/validate/mod.rs @@ -6,8 +6,9 @@ use crate::{ traits::{PoolTransaction, TransactionOrigin}, }; use reth_primitives::{ - Address, BlobTransactionSidecar, IntoRecoveredTransaction, SealedBlock, - TransactionSignedEcRecovered, TxHash, B256, U256, + Address, BlobTransactionSidecar, IntoRecoveredTransaction, + PooledTransactionsElementEcRecovered, SealedBlock, TransactionSignedEcRecovered, TxHash, B256, + U256, }; use std::{fmt, future::Future, time::Instant}; @@ -152,7 +153,7 @@ 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; /// Validates the transaction and returns a [`TransactionValidationOutcome`] describing the /// validity of the given transaction.