diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index f7211489e93a..670ee7f352ef 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -846,6 +846,34 @@ impl TransactionSigned { &self.transaction } + /// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`]. + /// + /// This function used as a helper to convert from a decoded p2p broadcast message to + /// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on + /// p2p, return an err if `tx` is [`Transaction::Eip4844`]. + pub fn try_into_pooled(self) -> Result { + let hash = self.hash(); + match self { + Self { transaction: Transaction::Legacy(tx), signature, .. } => { + Ok(PooledTransactionsElement::Legacy(Signed::new_unchecked(tx, signature, hash))) + } + Self { transaction: Transaction::Eip2930(tx), signature, .. } => { + Ok(PooledTransactionsElement::Eip2930(Signed::new_unchecked(tx, signature, hash))) + } + Self { transaction: Transaction::Eip1559(tx), signature, .. } => { + Ok(PooledTransactionsElement::Eip1559(Signed::new_unchecked(tx, signature, hash))) + } + Self { transaction: Transaction::Eip7702(tx), signature, .. } => { + Ok(PooledTransactionsElement::Eip7702(Signed::new_unchecked(tx, signature, hash))) + } + // Not supported because missing blob sidecar + tx @ Self { transaction: Transaction::Eip4844(_), .. } => Err(tx), + #[cfg(feature = "optimism")] + // Not supported because deposit transactions are never pooled + tx @ Self { transaction: Transaction::Deposit(_), .. } => Err(tx), + } + } + /// Transaction hash. Used to identify transaction. pub fn hash(&self) -> TxHash { *self.tx_hash() diff --git a/crates/primitives/src/transaction/pooled.rs b/crates/primitives/src/transaction/pooled.rs index 93a3c1823224..eea10d44c9f8 100644 --- a/crates/primitives/src/transaction/pooled.rs +++ b/crates/primitives/src/transaction/pooled.rs @@ -46,34 +46,6 @@ pub enum PooledTransactionsElement { } impl PooledTransactionsElement { - /// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`]. - /// - /// This function used as a helper to convert from a decoded p2p broadcast message to - /// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on - /// p2p, return an err if `tx` is [`Transaction::Eip4844`]. - pub fn try_from_broadcast(tx: TransactionSigned) -> Result { - let hash = tx.hash(); - match tx { - TransactionSigned { transaction: Transaction::Legacy(tx), signature, .. } => { - Ok(Self::Legacy(Signed::new_unchecked(tx, signature, hash))) - } - TransactionSigned { transaction: Transaction::Eip2930(tx), signature, .. } => { - Ok(Self::Eip2930(Signed::new_unchecked(tx, signature, hash))) - } - TransactionSigned { transaction: Transaction::Eip1559(tx), signature, .. } => { - Ok(Self::Eip1559(Signed::new_unchecked(tx, signature, hash))) - } - TransactionSigned { transaction: Transaction::Eip7702(tx), signature, .. } => { - Ok(Self::Eip7702(Signed::new_unchecked(tx, signature, hash))) - } - // Not supported because missing blob sidecar - tx @ TransactionSigned { transaction: Transaction::Eip4844(_), .. } => Err(tx), - #[cfg(feature = "optimism")] - // Not supported because deposit transactions are never pooled - tx @ TransactionSigned { transaction: Transaction::Deposit(_), .. } => Err(tx), - } - } - /// Converts from an EIP-4844 [`RecoveredTx`] to a /// [`PooledTransactionsElementEcRecovered`] with the given sidecar. /// @@ -650,7 +622,7 @@ impl TryFrom for PooledTransactionsElement { type Error = TransactionConversionError; fn try_from(tx: TransactionSigned) -> Result { - Self::try_from_broadcast(tx).map_err(|_| TransactionConversionError::UnsupportedForP2P) + tx.try_into_pooled().map_err(|_| TransactionConversionError::UnsupportedForP2P) } } @@ -679,7 +651,7 @@ impl<'a> arbitrary::Arbitrary<'a> for PooledTransactionsElement { // Attempt to create a `TransactionSigned` with arbitrary data. let tx_signed = TransactionSigned::arbitrary(u)?; // Attempt to create a `PooledTransactionsElement` with arbitrary data, handling the Result. - match Self::try_from_broadcast(tx_signed) { + match tx_signed.try_into_pooled() { Ok(tx) => Ok(tx), Err(tx) => { let (tx, sig, hash) = tx.into_parts(); diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 2859d71b9d11..a0d4d40983e4 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -1267,7 +1267,8 @@ impl PoolTransaction for EthPooledTransaction { tx: RecoveredTx, ) -> Result, Self::TryFromConsensusError> { let (tx, signer) = tx.to_components(); - let pooled = PooledTransactionsElement::try_from_broadcast(tx) + let pooled = tx + .try_into_pooled() .map_err(|_| TryFromRecoveredTransactionError::BlobSidecarMissing)?; Ok(RecoveredTx::from_signed_transaction(pooled, signer)) }