diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index fd973ff741..b2ff09300f 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -91,11 +91,12 @@ pub use storage::StorageEntry; pub use transaction::{ util::secp256k1::{public_key_to_address, recover_signer, sign_message}, AccessList, AccessListItem, AccessListWithGasUsed, BlobTransaction, BlobTransactionSidecar, - FromRecoveredTransaction, IntoRecoveredTransaction, InvalidTransactionError, - PooledTransactionsElement, PooledTransactionsElementEcRecovered, Signature, Transaction, - TransactionKind, TransactionMeta, TransactionSigned, TransactionSignedEcRecovered, - TransactionSignedNoHash, TxEip1559, TxEip2930, TxEip4844, TxLegacy, TxType, EIP1559_TX_TYPE_ID, - EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID, + FromRecoveredPooledTransaction, FromRecoveredTransaction, IntoRecoveredTransaction, + InvalidTransactionError, PooledTransactionsElement, PooledTransactionsElementEcRecovered, + Signature, Transaction, TransactionKind, TransactionMeta, TransactionSigned, + TransactionSignedEcRecovered, TransactionSignedNoHash, TxEip1559, TxEip2930, TxEip4844, + TxLegacy, TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, + LEGACY_TX_TYPE_ID, }; pub use withdrawal::Withdrawal; diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 115bc83aea..e65297ea72 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1089,6 +1089,16 @@ impl FromRecoveredTransaction for TransactionSignedEcRecovered { } } +/// A transaction type that can be created from a [`PooledTransactionsElementEcRecovered`] +/// transaction. +/// +/// This is a conversion trait that'll ensure transactions received via P2P can be converted to the +/// transaction type that the transaction pool uses. +pub trait FromRecoveredPooledTransaction { + /// Converts to this type from the given [`PooledTransactionsElementEcRecovered`]. + fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self; +} + /// The inverse of [`FromRecoveredTransaction`] that ensure the transaction can be sent over the /// network pub trait IntoRecoveredTransaction { diff --git a/crates/primitives/src/transaction/pooled.rs b/crates/primitives/src/transaction/pooled.rs index bcc870a2c0..a293b0b67a 100644 --- a/crates/primitives/src/transaction/pooled.rs +++ b/crates/primitives/src/transaction/pooled.rs @@ -373,6 +373,12 @@ impl PooledTransactionsElementEcRecovered { self.transaction } + /// Transform back to [`PooledTransactionsElement`] + pub fn into_ecrecovered_transaction(self) -> TransactionSignedEcRecovered { + let (tx, signer) = self.into_components(); + tx.into_ecrecovered_transaction(signer) + } + /// Desolve Self to its component pub fn into_components(self) -> (PooledTransactionsElement, Address) { (self.transaction, self.signer) diff --git a/crates/transaction-pool/src/test_utils/mock.rs b/crates/transaction-pool/src/test_utils/mock.rs index 19d663593c..b9acbfe7a4 100644 --- a/crates/transaction-pool/src/test_utils/mock.rs +++ b/crates/transaction-pool/src/test_utils/mock.rs @@ -12,10 +12,10 @@ use rand::{ prelude::Distribution, }; use reth_primitives::{ - constants::MIN_PROTOCOL_BASE_FEE, hex, Address, FromRecoveredTransaction, - IntoRecoveredTransaction, Signature, Transaction, TransactionKind, TransactionSigned, - TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxHash, TxLegacy, TxType, H256, - U128, U256, + constants::MIN_PROTOCOL_BASE_FEE, hex, Address, FromRecoveredPooledTransaction, + FromRecoveredTransaction, IntoRecoveredTransaction, PooledTransactionsElementEcRecovered, + Signature, Transaction, TransactionKind, TransactionSigned, TransactionSignedEcRecovered, + TxEip1559, TxEip2930, TxEip4844, TxHash, TxLegacy, TxType, H256, U128, U256, }; use std::{ops::Range, sync::Arc, time::Instant}; @@ -523,6 +523,12 @@ impl FromRecoveredTransaction for MockTransaction { } } +impl FromRecoveredPooledTransaction for MockTransaction { + fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self { + FromRecoveredTransaction::from_recovered_transaction(tx.into_ecrecovered_transaction()) + } +} + impl IntoRecoveredTransaction for MockTransaction { fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { let tx = Transaction::Legacy(TxLegacy { diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index cc7f1ae8a5..eb41edd4b7 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -6,8 +6,9 @@ use crate::{ }; use futures_util::{ready, Stream}; use reth_primitives::{ - Address, BlobTransactionSidecar, FromRecoveredTransaction, IntoRecoveredTransaction, PeerId, - PooledTransactionsElement, PooledTransactionsElementEcRecovered, Transaction, TransactionKind, + Address, BlobTransactionSidecar, FromRecoveredPooledTransaction, FromRecoveredTransaction, + IntoRecoveredTransaction, PeerId, PooledTransactionsElement, + PooledTransactionsElementEcRecovered, Transaction, TransactionKind, TransactionSignedEcRecovered, TxHash, EIP1559_TX_TYPE_ID, EIP4844_TX_TYPE_ID, H256, U256, }; use reth_rlp::Encodable; @@ -511,7 +512,12 @@ impl BestTransactions for std::iter::Empty { /// Trait for transaction types used inside the pool pub trait PoolTransaction: - fmt::Debug + Send + Sync + FromRecoveredTransaction + IntoRecoveredTransaction + fmt::Debug + + Send + + Sync + + FromRecoveredPooledTransaction + + FromRecoveredTransaction + + IntoRecoveredTransaction { /// Hash of the transaction. fn hash(&self) -> &TxHash; @@ -758,6 +764,12 @@ impl FromRecoveredTransaction for EthPooledTransaction { } } +impl FromRecoveredPooledTransaction for EthPooledTransaction { + fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self { + EthPooledTransaction::from(tx) + } +} + impl IntoRecoveredTransaction for EthPooledTransaction { fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { self.transaction.clone()