Skip to content

Commit 9b28935

Browse files
authoredNov 23, 2024
feat: use defined pool type internally (paradigmxyz#12803)
1 parent 7c5cb90 commit 9b28935

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed
 

‎crates/transaction-pool/src/pool/mod.rs

+34-50
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ use crate::{
7878
PoolTransaction, PropagatedTransactions, TransactionOrigin,
7979
},
8080
validate::{TransactionValidationOutcome, ValidPoolTransaction},
81-
CanonicalStateUpdate, PoolConfig, TransactionOrdering, TransactionValidator,
81+
CanonicalStateUpdate, EthPoolTransaction, PoolConfig, TransactionOrdering,
82+
TransactionValidator,
8283
};
8384
use alloy_primitives::{Address, TxHash, B256};
8485
use best::BestTransactions;
@@ -87,9 +88,7 @@ use reth_eth_wire_types::HandleMempoolData;
8788
use reth_execution_types::ChangedAccount;
8889

8990
use alloy_eips::eip4844::BlobTransactionSidecar;
90-
use reth_primitives::{
91-
BlobTransaction, PooledTransactionsElement, TransactionSigned, TransactionSignedEcRecovered,
92-
};
91+
use reth_primitives::PooledTransactionsElement;
9392
use std::{
9493
collections::{HashMap, HashSet},
9594
fmt,
@@ -312,18 +311,33 @@ where
312311
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).take(max).collect()
313312
}
314313

315-
/// Returns the [`BlobTransaction`] for the given transaction if the sidecar exists.
314+
/// Converts the internally tracked transaction to the pooled format.
316315
///
317-
/// Caution: this assumes the given transaction is eip-4844
318-
fn get_blob_transaction(&self, transaction: TransactionSigned) -> Option<BlobTransaction> {
319-
if let Ok(Some(sidecar)) = self.blob_store.get(transaction.hash()) {
320-
if let Ok(blob) =
321-
BlobTransaction::try_from_signed(transaction, Arc::unwrap_or_clone(sidecar))
322-
{
323-
return Some(blob)
324-
}
316+
/// If the transaction is an EIP-4844 transaction, the blob sidecar is fetched from the blob
317+
/// store and attached to the transaction.
318+
fn to_pooled_transaction(
319+
&self,
320+
transaction: Arc<ValidPoolTransaction<T::Transaction>>,
321+
) -> Option<<<V as TransactionValidator>::Transaction as PoolTransaction>::Pooled>
322+
where
323+
<V as TransactionValidator>::Transaction: EthPoolTransaction,
324+
{
325+
if transaction.is_eip4844() {
326+
let sidecar = self.blob_store.get(*transaction.hash()).ok()??;
327+
transaction.transaction.clone().try_into_pooled_eip4844(sidecar)
328+
} else {
329+
transaction
330+
.transaction
331+
.clone()
332+
.try_into_pooled()
333+
.inspect_err(|err| {
334+
debug!(
335+
target: "txpool", %err,
336+
"failed to convert transaction to pooled element; skipping",
337+
);
338+
})
339+
.ok()
325340
}
326-
None
327341
}
328342

329343
/// Returns converted [`PooledTransactionsElement`] for the given transaction hashes.
@@ -333,39 +347,19 @@ where
333347
limit: GetPooledTransactionLimit,
334348
) -> Vec<PooledTransactionsElement>
335349
where
336-
<V as TransactionValidator>::Transaction:
337-
PoolTransaction<Consensus: Into<TransactionSignedEcRecovered>>,
350+
<V as TransactionValidator>::Transaction: EthPoolTransaction,
338351
{
339352
let transactions = self.get_all(tx_hashes);
340353
let mut elements = Vec::with_capacity(transactions.len());
341354
let mut size = 0;
342355
for transaction in transactions {
343356
let encoded_len = transaction.encoded_length();
344-
let recovered: TransactionSignedEcRecovered =
345-
transaction.transaction.clone().into_consensus().into();
346-
let tx = recovered.into_signed();
347-
let pooled = if tx.is_eip4844() {
348-
// for EIP-4844 transactions, we need to fetch the blob sidecar from the blob store
349-
if let Some(blob) = self.get_blob_transaction(tx) {
350-
PooledTransactionsElement::BlobTransaction(blob)
351-
} else {
352-
continue
353-
}
354-
} else {
355-
match PooledTransactionsElement::try_from(tx) {
356-
Ok(element) => element,
357-
Err(err) => {
358-
debug!(
359-
target: "txpool", %err,
360-
"failed to convert transaction to pooled element; skipping",
361-
);
362-
continue
363-
}
364-
}
357+
let Some(pooled) = self.to_pooled_transaction(transaction) else {
358+
continue;
365359
};
366360

367361
size += encoded_len;
368-
elements.push(pooled);
362+
elements.push(pooled.into());
369363

370364
if limit.exceeds(size) {
371365
break
@@ -381,19 +375,9 @@ where
381375
tx_hash: TxHash,
382376
) -> Option<PooledTransactionsElement>
383377
where
384-
<V as TransactionValidator>::Transaction:
385-
PoolTransaction<Consensus: Into<TransactionSignedEcRecovered>>,
378+
<V as TransactionValidator>::Transaction: EthPoolTransaction,
386379
{
387-
self.get(&tx_hash).and_then(|transaction| {
388-
let recovered: TransactionSignedEcRecovered =
389-
transaction.transaction.clone().into_consensus().into();
390-
let tx = recovered.into_signed();
391-
if tx.is_eip4844() {
392-
self.get_blob_transaction(tx).map(PooledTransactionsElement::BlobTransaction)
393-
} else {
394-
PooledTransactionsElement::try_from(tx).ok()
395-
}
396-
})
380+
self.get(&tx_hash).and_then(|tx| self.to_pooled_transaction(tx).map(Into::into))
397381
}
398382

399383
/// Updates the entire pool after a new block was executed.

‎crates/transaction-pool/src/traits.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ impl BestTransactionsAttributes {
932932
/// a subset of the `Pooled` format.
933933
pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
934934
/// Associated error type for the `try_from_consensus` method.
935-
type TryFromConsensusError;
935+
type TryFromConsensusError: fmt::Display;
936936

937937
/// Associated type representing the raw consensus variant of the transaction.
938938
type Consensus: From<Self> + TryInto<Self, Error = Self::TryFromConsensusError>;
@@ -955,6 +955,11 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
955955
pooled.into()
956956
}
957957

958+
/// Tries to convert the `Consensus` type into the `Pooled` type.
959+
fn try_into_pooled(self) -> Result<Self::Pooled, Self::TryFromConsensusError> {
960+
Self::try_consensus_into_pooled(self.into_consensus())
961+
}
962+
958963
/// Tries to convert the `Consensus` type into the `Pooled` type.
959964
fn try_consensus_into_pooled(
960965
tx: Self::Consensus,
@@ -1084,7 +1089,9 @@ pub trait EthPoolTransaction:
10841089
Consensus: From<TransactionSignedEcRecovered>
10851090
+ Into<TransactionSignedEcRecovered>
10861091
+ Into<TransactionSigned>,
1087-
Pooled: From<PooledTransactionsElementEcRecovered> + Into<PooledTransactionsElementEcRecovered>,
1092+
Pooled: From<PooledTransactionsElementEcRecovered>
1093+
+ Into<PooledTransactionsElementEcRecovered>
1094+
+ Into<PooledTransactionsElement>,
10881095
>
10891096
{
10901097
/// Extracts the blob sidecar from the transaction.

0 commit comments

Comments
 (0)
Please sign in to comment.